du8980919 2015-04-04 04:44
浏览 101
已采纳

从数据库中检索特定行并显示特定行中的所有数据并使用php发送json

I would like to retrieve a specific row from database using php and mysqli.

For example:

I want to retrieve the row of data with userid =2

$userid =2;

Then I get it the user email, name, password from database using the $userid given:

include("includes/connect.php");
$user = "SELECT * FROM account WHERE user_id = $userid " ;
$query = mysqli_query ($conn,  $user);        

while($result = mysqli_fetch_array ($query)){
      $name = $result['username'];
      $password = $result['user_password']; 
      $email = $result['user_email'];           
  }

Then I would like to send the username, password and email of the userid=2 to mobile app in json.

How can I do that after that?

  • 写回答

2条回答 默认 最新

  • duanhuang4306 2015-04-04 04:48
    关注

    You need to pass data with json_encode:

    $data = array();
    while($result = mysqli_fetch_array ($query)){
      $data['username'] = $result['username'];
      $data['user_password'] = $result['user_password']; 
      $data['user_email'] = $result['user_email'];           
    }
    
    echo json_encode($data);
    

    Also you can use mysqli_fetch_assoc for retrieving json data from database:

    $row=mysqli_fetch_assoc($query)
    echo json_encode($row);
    

    More information about mysqli_fetch_assoc function.

    Note, If you are sure that just one row will return(In this case I think just one row will return, because usually user_id is unique), no need to use while.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊