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 WPF RichTextBox格式化大量文本卡顿如何解决
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 pip install后修改模块路径,import失败,需要在哪里修改环境变量?
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决