PHP函数总是返回false

时间:2014-04-25 18:06:51

标签: php mysql json

我希望找出返回结果始终为false

的原因

SQL和PHP代码如下:

public function getAlbumList($userID){
    $albumIDs= mysql_query("SELECT albumID FROM albumAccess WHERE userID='$userID'");
    //$albums = mysql_query("SELECT albums.* FROM albums WHERE albumAccess.userID = '$userID' AND albumAcess.albumID = albums.albumID");

    if($albumIDs){
        $albums=array();

        while($r = mysql_fetch_array($albumsIDs)){
            $albums[]=mysql_query("SELECT * FROM albums WHERE albumID = '$r'");     
        }
        return mysql_fetch_array($albums);

    }else{
        return false;
    }

}

获取结果并将其传递给Android应用程序的PHP代码是:

$userID=$_POST['userID'];

        $albumList = $db->getAlbumList($userID);

        if($albumList){
            $response["success"]=1;
            $response['album']=array();     
            while($r = mysql_fetch_assoc($albumList)){
                $response['album'][]= $r;                   
            }

            header('Content-type: text/json');
            echo json_encode($response);

        } else {
            //album list failed to be loaded
            $response["error"]=2;
            $response["error_msg"]="Error Loading Album List. Please try again later.";

            header('Content-type: text/json');
            echo json_encode($response);
        }           

当我检查返回的日志时,我总是收到错误消息

  

“加载专辑列表时出错”

但是,当我试图返回数组时,我才会得到它。如果我只返回true而不使用第二个SQL requset,则返回true。

4 个答案:

答案 0 :(得分:0)

你在这里做错了,看看这个改变

$albumIDs= mysql_fetch_row(mysql_query("SELECT albumID FROM albumAccess WHERE userID='$userID'"));  //changed this line

    while($r = mysql_fetch_array($albumsIDs)){
           $row=mysql_query("SELECT * FROM albums WHERE albumID = '$r'");
         $albums[]=mysql_fetch_row($row);  //added this line
    }

答案 1 :(得分:0)

问题在于第一个功能:

public function getAlbumList($userID){
    $albumIDs= mysql_query("SELECT albumID FROM albumAccess WHERE userID='$userID'");
    //$albums = mysql_query("SELECT albums.* FROM albums WHERE albumAccess.userID = '$userID' AND albumAcess.albumID = albums.albumID");

    if($albumIDs){
        $albums=array();

        while($r = mysql_fetch_array($albumsIDs)){
            $statement=mysql_query("SELECT * FROM albums WHERE albumID = '$r'");   
            $albums[]=mysql_fetch_row($statement); 
        }
        return $albums;

    }else{
        return false;
    }

}

以下行未正确获取用户ID:

$albums[]=mysql_query("SELECT * FROM albums WHERE albumID = '$r'"); 

但你必须使用:

$albums[]=mysql_query("SELECT * FROM albums WHERE albumID = '".$r['albumID']."'"); 

在此处更改:

while($r = mysql_fetch_array($albumsIDs)){
    $statement=mysql_query("SELECT * FROM albums WHERE albumID = '$r'");   
    $albums[]=mysql_fetch_row($statement); 
}
return $albums;

再见! 马可

答案 2 :(得分:0)

尝试此功能

public function getAlbumList($userID){
  $albumIDs= mysql_query("SELECT albumID FROM albumAccess WHERE userID='$userID'");


if($albumIDs){
    $albums=array();

    while($r = mysql_fetch_array($albumsIDs)){
        $second = mysql_query("SELECT * FROM albums WHERE albumID = '".$r['albumID']."' ");  
         $alb = mysql_fetch_array($second) ;
        $albums[]= $alb['your_column_you_want_fetch'];  
    }
    return $albums;

}else{
    return false;
}

}

答案 3 :(得分:0)

您也可以尝试使用JOIN或SUBQUERY:

public function getAlbumList($userID){
    $albumIDs= mysql_query("SELECT *
                            FROM albums
                            WHERE albumID IN (
                               SELECT albumID FROM albumAccess WHERE userID='$userID'
                            )");

    if(mysql_numrows($albumIDs) > 0){
        $albums=array();

        while($r = mysql_fetch_array($albumsIDs)){
            $albums[] = $r;    
        }

        return $albums;
    }

    return false;
}

PHP代码:

    $albumList = $db->getAlbumList($userID);

    if($albumList){
        $response["success"]=1;
        $response['album'] = $albumList;
    } else {
        //album list failed to be loaded
        $response["error"]=2;
        $response["error_msg"]="Error Loading Album List. Please try again later.";
    }   
    header('Content-type: text/json');
    echo json_encode($response);