2014-09-06 91 views
1

我怎麼可能通過Ajax請求獲取用戶1和用戶2的信息。我必須做兩個單獨的查詢嗎?讓兩個用戶都使用Ajax

$check = "SELECT * FROM user WHERE id='$user1_id'"; 
$check1 = mysqli_query($mysqli,$check); 
$resultArr = mysqli_fetch_array($check1); 
$json['username'] = ucfirst($resultArr['username']); 
$json['id'] = $resultArr['id']; 
$json['first'] = ucfirst($resultArr['first']); 
$json['middle'] = ucfirst($resultArr['middle']); 
$json['last'] = ucfirst($resultArr['last']); 
mysqli_free_result($check1); 

$check2 = "SELECT * FROM user WHERE id='$user2_id'"; 
$check12 = mysqli_query($mysqli,$check2); 
$resultArr2 = mysqli_fetch_array($check12); 
$json['username'] = ucfirst($resultArr2['username']); 
$json['id'] = $resultArr2['id']; 
$json['first'] = ucfirst($resultArr2['first']); 
$json['middle'] = ucfirst($resultArr2['middle']); 
$json['last'] = ucfirst($resultArr2['last']); 
mysqli_free_result($check2); 

並返回時,我有 - '+datamessage['first']+' >> '+datamessage['first']+' 問題是,他們依賴於誰在發送的消息,以便用戶2的名稱可能是第一或user1的可先掉在服務器端位置。

+1

爲什麼不能在同一個查詢中獲取兩個用戶? SELECT * FROM user WHERE id IN('$ user1_id','$ user2_id') – user3332631 2014-09-06 11:53:13

回答

1
$check = "SELECT * FROM user WHERE id in ('$user1_id','$user1_id')"; 
    $res= mysqli_query($mysqli,$check); 
    $i=0; 
    while($resultArr = mysqli_fetch_array($res)) 
    { 
     $json[$i]['username'] = ucfirst($resultArr['username']); 
     $json[$i]['id'] = $resultArr['id']; 
     $json[$i]['first'] = ucfirst($resultArr['first']); 
     $json[$i]['middle'] = ucfirst($resultArr['middle']); 
     $json[$i]['last'] = ucfirst($resultArr['last']); 
     $i++; 
} 
return json_decode($json); 
//hope it will work for you 
2
$check = "SELECT * FROM user WHERE id='$user1_id'"; 
$check1 = mysqli_query($mysqli,$check); 
$resultArr = mysqli_fetch_array($check1); 
$json1['username'] = ucfirst($resultArr['username']); 
$json1['id'] = $resultArr['id']; 
$json1['first'] = ucfirst($resultArr['first']); 
$json1['middle'] = ucfirst($resultArr['middle']); 
$json1['last'] = ucfirst($resultArr['last']); 
mysqli_free_result($check1); 

$check2 = "SELECT * FROM user WHERE id='$user2_id'"; 
$check12 = mysqli_query($mysqli,$check2); 
$resultArr2 = mysqli_fetch_array($check12); 
$json2['username'] = ucfirst($resultArr2['username']); 
$json2['id'] = $resultArr2['id']; 
$json2['first'] = ucfirst($resultArr2['first']); 
$json2['middle'] = ucfirst($resultArr2['middle']); 
$json2['last'] = ucfirst($resultArr2['last']); 
mysqli_free_result($check2); 

$json = array(
    'user1' => $json1, 
    'user2' => $json2, 
); 
+0

太棒了@Zoerji。所以,如果我想抓住服務器端的信息,我應該做''+ datamessage ['user2.first'] +'' – dave 2014-09-06 11:50:35

+0

@Tristup你應該做datamessage ['user2'] ['first']或datamessage.user2 。第一 – Zorji 2014-09-12 04:40:26

相關問題