2014-04-25 158 views
1

我期待找出原因返回的結果總是falsePHP函數總是返回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; 
    } 

} 

的PHP代碼以結果和傳遞與Android應用是這樣的:

$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請求,它將返回true。

+0

首先,你肯定有什麼事實際返回?另外,也許添加一個「或死(json_encode(array('errors'=> array('Internal issue:'=> mysql_error($ conn)))));」在mysql_query之後查看數據庫連接是否存在問題 – Guardanis

+0

是的,我正在查看錶格並確保發送的用戶標識符匹配什麼以及專輯ID以及 – user3108923

回答

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); 
+0

非常感謝。我現在獲得成功,但是我的陣列出來了0 – user3108923

+0

哦,讓我改變你的代碼的顯示部分。 – stomo21

+0

感謝您的幫助! – user3108923

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 
    } 
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; 

再見! 馬爾科

+0

第二部分是否沒有錯?我仍然得到相同的錯誤信息,這意味着它返回的是錯誤的 – user3108923

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; 
} 

}