我正在使用 MySQL 在数据库中存储项目列表,并尝试从单个表中检索所有项目。连接部分工作正常,检索项目似乎工作得很好,但我无法对项目列表进行 json_encode。
// Executes SQL query on the database specified by $con
$sql = "SELECT * FROM productlist";
$query = mysqli_query($con, $sql) or die(nl2br("\n Failed to execute query"));
// Retrieves all the rows returned by the SQL query
$rows = array();
while($r = mysqli_fetch_assoc($query)) {
$rows[] = $r;
}
echo json_encode($rows[0]); // test whether retrieval works
// Displays rows in content-type JSON and in PRETTY_PRINT
header('Content-Type: application/json');
echo json_encode($rows, JSON_PRETTY_PRINT);
Run Code Online (Sandbox Code Playgroud)
这是代码。的json_encode效果rows[0]很好。但是,当我将其注释掉并尝试对 执行相同操作时rows,它什么也没有返回。
它甚至没有返回错误,它执行得很好,只是json_encode当我尝试使用rows.
我究竟做错了什么?
| 归档时间: |
|
| 查看次数: |
9681 次 |
| 最近记录: |