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条)

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1