鍍金池/ 問答/PHP/ 我這么寫PHP后臺(tái)接口為什么只能返回?cái)?shù)據(jù)庫(kù)一條數(shù)據(jù) 我要的是返回跟uphonen

我這么寫PHP后臺(tái)接口為什么只能返回?cái)?shù)據(jù)庫(kù)一條數(shù)據(jù) 我要的是返回跟uphonenumber相匹配的所有數(shù)據(jù)

<?php
    header("Content-Type:application/json;charset=UTF-8");
    $uphonenumber = $_POST['phonenumber'];//post獲得手機(jī)號(hào)碼表單值
    $json = '';
    $data = array();
    include('init.php');//鏈接數(shù)據(jù)庫(kù)
    
    //$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);//執(zhí)行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();//關(guān)閉數(shù)據(jù)庫(kù)
?>
回答
編輯回答
她愚我

你的while循環(huán)里的賦值有問題,一直在重復(fù)為同一個(gè)鍵名賦值。
正確的應(yīng)該是:

$data[] = [
    'uid'      => $row["uid"],
    'pcontent' => $row["pcontent"],
    'src'      => explode(",",$row["ppic"]),
    'pdate'    => $row["pdate"],
    'pweek'    => $row["pweek"],
    'pweather' => $row["pweather"]
];

這種形式;

2017年4月12日 04:51
編輯回答
凝雅

循環(huán)寫法有問題。你這樣寫,當(dāng)前數(shù)據(jù)都會(huì)把之前的數(shù)據(jù)覆蓋掉,最后只返回最后一條數(shù)據(jù)

2018年3月6日 19:46