让两个用户都使用Ajax


Getting both users using Ajax

我怎么可能通过Ajax请求获得用户1和用户2的信息。我必须这样做两个单独的查询吗?

$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的名字可能是第一个,用户1的名字可能第一个。

$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,
);
  $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