我需要将 php 从数据库中提取的数据导出到 json 文件中,以便在 json 文件中输出,如下所示。我需要在每行之间换行。
"ID":"ID。", "姓名":"姓名",
这是我的查询,它已经有 id 和名称。但我不知道如何将它们导出为文件。
while($row = mysql_fetch_array($result))
{
echo $row['id'];
echo $row['name'];
}
最佳答案
$data = [];
while($row = mysql_fetch_array()) {
$data[] = $row;
}
$txt = json_encode($data);
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
答案将是这样的:
[
{ "id": "idvalue", "name": "namevalue" },
...
]
关于javascript - 将查询结果导出到json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156885/