weixin_33696106
weixin_33696106
2014-09-06 11:40
采纳率: 0%
浏览 12

让两个用户都使用Ajax

How is it possible for me to get user one and user 2's information back through and Ajax request. Must I do two separate queries like so?

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

And when returned I have - '+datamessage['first']+' >> '+datamessage['first']+' problem is they swap positions in the server side depending on who's sent the message so user 2's name could be first or user1's could be first.

图片转代码服务由CSDN问答提供 功能建议

                    

我怎么可能通过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 =“选择*从用户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的名称可以是第一名。     

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_33735676
    weixin_33735676 2014-09-06 11:43
    $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,
    );
    
    点赞 评论
  • weixin_33729196
    weixin_33729196 2014-09-06 12:14
      $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
    
    点赞 评论

相关推荐