2015-02-11 21 views
-1

嗨,在下面它返回成功的消息,但我沒有得到id,groupnames。 如何在下面的代碼中執行select查詢。我將記錄id和groupname的詳細信息。如何在php中顯示查詢記錄

任何一個可以幫助我

PHP

case "DispalyGroupDetails": 
     $userId = authenticateUser($db, $username, $password); 

     if ($userId != NULL) 

     { 

      if (isset($_REQUEST['username']))   
      {    
       $username = $_REQUEST['username']; 



       $sql = "select Id from users where username='$username' limit 1"; 

       if ($result = $db->query($sql)) 

       { 
         if ($row = $db->fetchObject($result)) 

         {  

            $sql = "select g.id,g.groupname from `users` u, `friends` f,`group` g 
            where u.Id=f.providerId and f.providerId=g.providerId"; 
            echo $sql; 


            if ($db->query($sql)) 
            { 
             $out = SUCCESSFUL; 
            } 
            else 
            { 
              $out = FAILED; 
            } 

         } 
         else 
         { 
          $out = FAILED;      
         } 
       } 

       else 
       { 
         $out = FAILED; 
       }    
      } 

      else 
      { 
        $out = FAILED; 
      }   
     } 
     else 
     { 
      $out = FAILED; 
     } 
    break; 
+0

沒有必要那麼多,如果條件的,可以單用,如果檢查多個條件'&&'或''||等 – 2015-02-11 10:34:02

+0

$ SQL =「從選擇g.id,g.groupname' users' U,'friends'樓'group'克 \t \t \t \t \t \t \t \t \t其中u.Id = f.providerId和f.providerId = g.providerId「; json格式如何執行查詢 – user1 2015-02-11 10:36:23

回答

1

嘗試這樣的事情。您需要獲取查詢的內容,而不是僅僅查看查詢是否成功。

$sql = "select g.id,g.groupname from `users` u, `friends` f,`group` g 
           where u.Id=f.providerId and f.providerId=g.providerId"; 

echo $sql; 

$theResult = $db->query($sql); 

if ($theResult) { 
    $theRow = $db->fetchObject($theResult); 
    echo $theRow->id; 
    echo $theRow->groupname; 
    //Etc 
    $out = SUCCESSFUL; 
} else { 
    $out = FAILED; 
} 
+0

如何獲取id和groupnames的數組如何寫一個 – user1 2015-02-11 10:58:41

+1

while($ theRow = $ db-> fetchObject($ theResult)){ echo $ theRow-> id; echo $ theRow-> groupname; } – Oliverb 2015-02-11 12:19:52