我正在尝试使用 MySQL 从表中获取所有行并将其存储为 JSON 文件。现在唯一存储在 JSON 文件中的是表格的第一行。
代码如下:
$sql_query = "SELECT * FROM table";
$res_sql = mysql_query($sql_query) or die(mysql_error());
$arr = array();
if(mysql_num_rows($res_sql) > 0){
while($row_sql = mysql_fetch_assoc($res_sql)){
$arr = $row_sql;
$json = json_encode($arr);
$file = 'table.json';
file_put_contents($file, $json);
}
}
最佳答案
$sql_query = "SELECT * FROM table";
$res_sql = mysql_query($sql_query) or die(mysql_error());
$arr = array();
if(mysql_num_rows($res_sql) > 0){
while($row_sql = mysql_fetch_assoc($res_sql)){
$arr[] = $row_sql;
}
$json = json_encode($arr);
$file = 'table.json';
file_put_contents($file, $json);
}
使file_put_contents
在循环外
关于php - MySQL 到 JSON,只加载第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173077/