简体   繁体   English

如何在PHP中以json格式转换mysql查询的结果?

[英]How to convert result of mysql query in json format in PHP?

i want to convert the result of mysql query into format json 我想将mysql查询的结果转换为json格式

The query return a lot fo records but the print_r of json doesn't return anything 查询返回很多fo记录,但是json的print_r不返回任何内容

$sql="select concat(sig,doc)as Order,date,age from ordertable";
$res = $conn->query($sql);
while($row = $res->fetch_assoc()) 
    {
        $rows[] = $row;
    }

$json = json_decode($rows, true);
fwrite($handle, "json: ".print_r($json,true)." \n\n");

You need to change this below two lines as follows, 您需要按以下两行更改此内容,

$json = json_encode($rows); // to convert it into json internal array
//$json = json_encode($rows,JSON_FORCE_OBJECT); // to convert it json object
fwrite($handle, "json: $json \n\n");

This should do the trick. 这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM