如何在php中显示查询记录

时间:2015-02-11 10:21:28

标签: php mysql

嗨,在下面它会返回SUCCESSFUL消息,但我没有得到id,groupnames。 如何在下面的代码中执行select查询。我想记录id和groupname的详细信息。

任何人都可以帮助我

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;

1 个答案:

答案 0 :(得分: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;
}