2015-09-27 85 views
1

當我使用json_encode($response)時,它只返回查詢的第一行,如何讓它返回所有行?

$email = $_POST['email']; 
$password = $_POST['password']; 

main.php

// check for user 
$user = $db->getUserByEmailAndPassword($email, $password); 
if ($user != false) { 
    // user found 
    $response["error"] = FALSE; 
    $response["user"]["name"] = $user["name"]; 
    $response["user"]["email"] = $user["email"]; 
    $response["user"]["dega"] = $user["dega"]; 
    $response["user"]["salla"] = $user["salla"]; 
    $response["user"]["ora"] = $user["ora"]; 
    $response["user"]["lenda"] = $user["lenda"]; 
    $response["user"]["dita"] = $user["dita"]; 
    echo json_encode($response); 
} 

method.php

public function getUserByEmailAndPassword($email, $password) { 
    $result = mysql_query("SELECT U.name, U.email, U.password, F.dega, 
     O.salla, O.ora, O.lenda, O.dita FROM users U 
     INNER JOIN fakulteti F on U.id = F.studenti 
      INNER JOIN orari O on F.id = O.fakulteti WHERE email = '$email'") or die(mysql_error()); 

    $no_of_rows = mysql_num_rows($result); 
    if ($no_of_rows > 0) { 
     $result = mysql_fetch_array($result); 
     $encrypted_password = $result['password'] 
     if ($encrypted_password == $password) { 
      return $result; 
     } 
    } 
} 

的Android類,這是我收到如何在我的Android迴應:

JSONObject user = jObj.getJSONObject("user"); 
String name = user.getString("name"); 
String email = user.getString("email"); 
String dega = user.getString("dega"); 
String salla = user.getString("salla"); 
String ora = user.getString("ora"); 
String lenda = user.getString("lenda"); 
String dita = user.getString("dita"); 

回答

0

你是顯式創建一個數組來處理只有一個r ow在你的main.php中

你需要用while()(例如)循環遍歷結果來構建包含多行的數組。

但是,查詢看起來像它只帶來一條記錄......或者有更多的1名學生使用相同的電子郵件?

+0

該查詢對我的目的是正確的,因爲我測試了它,並且它給了我3行,但我不知道該如何做一段時間() –

+0

mysql_fetch_array()帶來了一行並將指針移動到了下一個... so like something 'while($ row = mysql_fetch_array($ result,MYSQL_NUM)){ printf(「ID:%s Name:%s」,$ row [0],$ row [1]) ; }' –

+0

它不工作:( –