我正在尝试将 mySQL 编码为 JSON。我已经尝试过这个流行的链接 JSON 编码 MySQL 结果,但答案对我不起作用。我会感谢你的帮助。
这是我的代码:
<html>
<body>
<?php
header('Content-Type: text/html; charset=utf-8');
$con=mysqli_connect("localhost","root","root","Theory");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT `questions`.`questionID` AS question,
`questions`.`questionText` AS questionText,
`questions`.`categoryID` AS categoryID,
`answers`.`answerID` AS answerID,
`answers`.`answerText` AS answerText,
`answers`.`isTrue` AS isTrue
FROM `questions`,`answers`
WHERE `questions`.`questionID`=`answers`.`questionID`");
if (!$result)
{
die('Error: ' . mysqli_error($con));
}
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows[] = $r;
}
print json_encode($rows);
mysqli_close($con);
?>
</body>
</head>
我得到:“[]”对不起,如果我在做一些愚蠢的事情,我是 php 新手。