启动一个新项目,我已经从弃用的mysql_query命令转移到新的mysqli类型参数。
代码如下:
<?php
$mysqli = new mysqli("hostname", "username", "password", "dbname");
// Check the connection
if (mysqli_connect_errno()) {
printf("Connect failed.");
exit();
}
$sql = $mysqli->query("SELECT * FROM SafariFinder_Directory");
while ($rows = $sql->fetch_assoc()) {
echo json_encode($rows);
}
echo $res;
$mysqli->close();
?>
这会得到以下输出:
{"id":"3","inGameName":"Syrinathos","friendCode":"0000 0000 0000","safariType":"Normal","safariSlot1":"Aipom","safariSlot2":"Kecleon","safariSlot3":""}
{"id":"2","inGameName":"Herschel","friendCode":"1234 12341 234","safariType":"Zombie","safariSlot1":"Crawler","safariSlot2":"Runner","safariSlot3":""}
{"id":"4","inGameName":"Syrinathos","friendCode":"0000 0000 0000","safariType":"Normal","safariSlot1":"Aipom","safariSlot2":"Kecleon","safariSlot3":"Ditto"}
{"id":"6","inGameName":"Kira","friendCode":"2345 2345 2345","safariType":"Ghost","safariSlot1":"Shuppet","safariSlot2":"Haunter","safariSlot3":"Gengar"}
缺少有效JSON的唯一组件是方括号和实际条目之间的逗号。非常感谢任何帮助。
答案 0 :(得分:4)
您应该将结果放入数组中,然后对数组进行编码。
$rows = array();
while ($row = $sql->fetch_assoc()) {
$rows[] = $row;
}
echo json_encode($rows);