PHP - while 循环中的 mysqli_fetch_assoc 不显示任何内容

标签 php mysql json

我想通过 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/

相关文章:

MySQL数据库设计: User and event table

mysql - mysql插入的格式化日期

java - 如何使用java为JSON中单个数组中的每个元素添加对象?

java - 如何在java中执行graphql?

java - 处理数组或对象的 json 字段?

php - CodeIgniter 使用 ActiveRecord 转义查询.. 不希望它

javascript - jQuery 仅替换新的选择框,而不替换之前的选择框

php - 从 View 文件夹中的文件导航到laravel中 View 文件夹中的另一个文件

javascript - 未捕获的类型错误 : Cannot read property 'SearchBox' of undefined

mysql - 将日期插入数据库时​​出现 SQL 语法错误(框架 : codeigniter)