我这么写PHP后台接口为什么只能返回数据库一条数据 我要的是返回跟uphonenumber相匹配的所有数据

<?php
    header("Content-Type:application/json;charset=UTF-8");
    $uphonenumber = $_POST['phonenumber'];//post获得手机号码表单值
    $json = '';
    $data = array();
    include('init.php');//链接数据库
    
    //$sql = "SELECT * FROM yffice_project where uphonenumber = $uphonenumber";
    $sql = "SELECT * FROM yffice_project where uphonenumber = $uphonenumber";
    mysql_query("set names utf8");
    $result = mysql_query($sql);//执行sql
    if($result){
    //echo "查询成功";
    while ($row = mysql_fetch_array($result))
    {
    $data["uid"]=$row["uid"];
    $data["pcontent"]=$row["pcontent"];
    $data["src"]=explode(",",$row["ppic"]);
    $data["pdate"]=$row["pdate"];
    $data["pweek"]=$row["pweek"];
    $data["pweather"]=$row["pweather"];
    }
    $json=json_encode($data);
    echo $json;
    }else{
    echo "查询失败";
    }
    mysql_close();//关闭数据库
?>
慕村9548890
浏览 490回答 2
2回答

GCT1015

你的while循环里的赋值有问题,一直在重复为同一个键名赋值。正确的应该是: $data[] = [ 'uid' => $row["uid"], 'pcontent' => $row["pcontent"], 'src' => explode(",",$row["ppic"]), 'pdate' => $row["pdate"], 'pweek' => $row["pweek"], 'pweather' => $row["pweather"] ]; 这种形式;

Smart猫小萌

循环写法有问题。你这样写,当前数据都会把之前的数据覆盖掉,最后只返回最后一条数据
打开App,查看更多内容
随时随地看视频慕课网APP