Javascript php中的MySql查询仅在用json编码时返回最后一个结果

Javascript php中的MySql查询仅在用json编码时返回最后一个结果,javascript,php,jquery,mysql,json,Javascript,Php,Jquery,Mysql,Json,我在php中有一个查询(mysql),当编码为json时,它只返回查询的最后一个结果,但是我确信查询返回多个结果: $counter=0; $reply=array(); $result = mysqli_query($conn,$stringQuery);; if ($result->num_rows > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) {

我在php中有一个查询(mysql),当编码为json时,它只返回查询的最后一个结果,但是我确信查询返回多个结果:

$counter=0;
$reply=array();
$result = mysqli_query($conn,$stringQuery);;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $reply["json"]=$row;
        $counter++;
    }

    echo json_encode($reply);
} else {
    echo json_encode("0 results");
}
$conn->close();
当我用javascript字符串化结果时,它只返回表的最后一个条目,或单个结果:

$.ajax({
    type: 'POST',
    url: 'SearchQuery.php',
    data: {
        'json': cond
    },
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
    dataType: 'json',
    success: function(response) {
        alert(JSON.stringify(response["json"]));

    }
});
试试这个:

while ($row = mysqli_fetch_assoc($result)) {
    $reply[] = $row;
    // ^^^^^^^^^^^^^

    $counter++;
}
echo json_encode($reply);
试试这个:

while ($row = mysqli_fetch_assoc($result)) {
    $reply[] = $row;
    // ^^^^^^^^^^^^^

    $counter++;
}
echo json_encode($reply);
尝试使用fetch_数组()更改fetch_assoc()

或者尝试:

while($row=$result->fetch_array()){
     $items.=<<<EOD
    {
"r": "{$row['yourfield']}",
},
EOD;
$counter++;
}

header('Content-type: application/json');
?>
{
    "response": [
        <?php echo $item;?>
    ]
}
while($row=$result->fetch\u array()){
$items.=尝试使用fetch_数组()更改fetch_assoc()

或者尝试:

while($row=$result->fetch_array()){
     $items.=<<<EOD
    {
"r": "{$row['yourfield']}",
},
EOD;
$counter++;
}

header('Content-type: application/json');
?>
{
    "response": [
        <?php echo $item;?>
    ]
}
while($row=$result->fetch\u array()){
$items.=此处

在这一行中,while循环中的最后一个fetch行将覆盖现有结果,因此它只显示最后一行作为结果

你可以试试

$reply["json"][]=$row; 
因此,在ajax的成功函数中

alert(JSON.stringify(response["json"]));
您可以通过键名“json”访问数据

在这一行中,while循环中的最后一个fetch行将覆盖现有结果,因此它只显示最后一行作为结果

你可以试试

$reply["json"][]=$row; 
因此,在ajax的成功函数中

alert(JSON.stringify(response["json"]));

您可以通过键名“json”访问数据。

如果我这样做,并将javascript中的警报更改为:alert(json.stringify(response));什么都没有发生,就没有警报;php中发生了一些事情,但不会出错presented@user1281678尝试
警报(JSON.stringify(响应))
因为您现在没有用
json
发送数据,正如我之前所说:如果我这样做,并将javascript中的警报更改为:alert(json.stringify(response));什么都没有发生,就没有警报;php中发生了一些事情,但是如果我这样做并将javascript中的警报更改为:alert(json.stringify(response));没有发生任何事情,没有警报;php中发生了一些事情,但是没有错误presented@user1281678尝试
alert(JSON.stringify(response));
因为您现在不是在
JSON
中发送数据,正如我之前所说:如果我这样做,并将javascript中的警报更改为:alert(JSON.stringify(response));没有发生任何事情,没有警报;php中发生了一些事情,但是没有出现错误。
$result=mysqli_query($conn,$stringQuery);
额外分号修复了它,但问题仍然存在。$result=mysqli_query($conn,$stringQuery);额外分号修复了它,但问题仍然存在$counter=0;//$reply=array();$result=mysqli_query($conn,$stringQuery);if($result->num_rows>0){//输出每行的数据,$row=mysqli_fetch_assoc($result)){$reply=array($counter=>$row);//$reply[]=$row;$counter++}echo json_encode($reply)}else{echo json_encode(“0结果”)}$conn->close();这应该可以工作,但不会出现错误???在console.log中???是否更改了$row['yourfield']使用你的fieldname???你确定EOD代码没有格式就写得正确吗???检查代码,因为代码是正确的!!!我在我的函数页中使用了它。对不起,我犯了错误。我现在已经尝试了,没有错误,并且在alert()中给出了响应[],Empty这是我与你的else一起编写的代码{$counter=0;/$reply=array()$result=mysqli_query($conn,$stringQuery);if($result->num_rows>0){//输出每行的数据,而($row=$result->fetch_array()){$items.=500错误不,您不能这样做……在插入ELSE代码之前,在响应中只写{“response”:[]}…现在请毫无例外地尝试此代码。$counter=0;//$reply=array();$result=mysqli_query($conn,$stringQuery);if($result->num_rows>0){//while($row=mysqli_fetch_assoc($result)){$reply=array($counter=>$row);//$reply[]=$row;$counter++;}echo json_encode($reply);}else{echo json_encode(“0结果”)}$conn->close();这应该可以工作,但不会出现错误???在console.log中???您是否更改了$row['yourfield'使用你的fieldname???你确定EOD代码没有格式就写得正确吗???检查代码,因为代码是正确的!!!我在我的函数页中使用了它。对不起,我犯了错误。我现在已经尝试了,没有错误,并且在alert()中给出了响应[],Empty这是我与你的else一起编写的代码{$counter=0;/$reply=array()$result=mysqli_query($conn,$stringQuery);if($result->num_rows>0){//输出每行的数据,而($row=$result->fetch_array()){$items.=500错误不,您不能这样做……在插入ELSE代码之前,在响应中只写{“response”:[]}..现在请毫无例外地尝试此代码..在ajax调用中更改contentType:“application/json”在ajax调用中更改contentType:“application/json”