Closed. This question is
off-topic。它目前不接受答案。
想改进这个问题吗?
Update the question所以堆栈溢出的值小于aa>。
四年前关闭。
所以,我有一个非常简单的测试数据库和一个简单的php脚本,可以将mysql数据库转换为JSON并将其输出到一个文件。问题是,JSON文件中的第一项是一个空数组,我似乎不明白为什么。以下是相关的PHP:
<?php
$host = "localhost";
$username = "visarts_testing";
$password = "*****";
$dbname = "visarts_test";
$tbl_name = "tested";
//open a connection to the database
$conn = mysqli_connect($host, $username, $password, $dbname);
if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$fname = $_POST['lname'];
$lname = $_POST['fname'];
$email = $_POST['email'];
$type = $_POST['type'];
$body = $_POST['body'];
//write the values from the html form into the specified rows of the specified table
$sql = "INSERT INTO $tbl_name (fname, lname, email, type, body)
VALUES('$fname', '$lname', '$email', '$type', '$body')";
if(mysqli_query($conn, $sql)) {
header("Location: output.html");
} else {
echo "Error: " . $sql . "<br />" . mysqli_error($conn);
}
//select all rows from the table
$sql = "select * from $tbl_name";
$result = mysqli_query($conn, $sql) or die("Error in selecting " . mysqli_error($conn));
//loop through the table rows and add them to an array
$arr[] = array();
while($row = mysqli_fetch_assoc($result))
{
$arr[] = $row;
}
//create a json file and write the array of table rows to it
$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($arr));
fclose($fp);
//close the db connection
mysqli_close($conn);
?>
这里是'data.JSON'文件中JSON的第一部分
[[],{"fname":"Brendan","lname":"Smith","email":"something@gmail.com","type":"poetry","body":"this is the story all about how"}......]
你知道为什么吗?
提前谢谢。