我正在尝试遍历 MySQL 对象并在另一个页面上使用 ajax 调用来附加数据,但我无法让 php 将有效的 JSON 返回给回调。
这个明显不行。。。
<?php
$db_host = "localhost";
$db_user = "blah";
$db_pass = "blah";
$db_name = "chat";
$mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
$myQuery = "SELECT * FROM users";
$result = $mysqli->query($myQuery) or die($mysqli->error);
$row = $result->fetch_assoc();
echo json_encode($row);
?>
或者这个...
<?php
$db_host = "localhost";
$db_user = "blah";
$db_pass = "blah";
$db_name = "chat";
$mysqli = new MySQLi($db_host, $db_user, $db_pass, $db_name);
$myQuery = "SELECT * FROM users";
$result = $mysqli->query($myQuery) or die($mysqli->error);
while ( $row = $result->fetch_assoc() ){
echo json_encode($row) . ", ";
}
?>