我想通过 JSON 将一些数据从 MySQL 数据库提供给 Android 应用程序。
到目前为止,我已经编写了这个 php 脚本来为我提供 JSON 对象:
$result = mysqli_query($con,"SELECT * FROM job");
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){
$output[] = $row;
}
print(json_encode($output));
我偶然发现,当我不添加行“$row = mysqli_fetch_assoc($result);”时在 while 循环之前它不会返回任何内容。但是当我像示例中那样添加这一行时,JSON 对象不包含第一个元素。
我相信这是因为 $row 已经包含第一行的这一行。
希望你能帮助我:)
最佳答案
为了找出查询出了什么问题,您必须执行以下操作:
if ($result = mysqli_query($db, $sql)) {
// Here you can see and check count of rows in your result set.
$numRows = mysqli_num_rows($result);
// ...
} else {
// Here you can see and check and understand error.
var_export(mysqli_error($db));
}
为了遍历结果集中的所有行,您必须执行以下操作:
while ($row = mysqli_fetch_assoc($result)) {
var_export($row);
}
并且 while 循环之前不能有 $row = mysqli_fetch_assoc($result)
。
关于PHP - while 循环中的 mysqli_fetch_assoc 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40164367/