php - 无法获取 Mysqli_result

标签 php mysqli

我有这个错误

Warning: mysqli_fetch_array() [function.mysqli-fetch-array]: Couldn't fetch mysqli_result in /home/fights7/public_html/include/load_more_home_posts.php on line 12

想知道我在下面的代码中做错了什么?

$articles_data = mysqli_query($mysqli,"SELECT * FROM streamdata WHERE streamitem_id < '$lastID' ORDER BY streamitem_id DESC LIMIT 10") or die(mysql_error());
while($articles_info = mysqli_fetch_array($articles_data)) {
$json = array();
$json['streamitem_id'] = $articles_info['streamitem_id'];
$json['streamitem_content'] = $articles_info['streamitem_content'];
$json['streamitem_timestamp'] = $articles_info['streamitem_timestamp'];
mysqli_free_result($articles_data);

最佳答案

马上,您似乎在获取循环中调用了 mysqli_free_result(),因此在第一次循环迭代之后,您的结果资源已关闭并释放,并且不会有更多结果可用.

while($articles_info = mysqli_fetch_array($articles_data)) {
  $json = array();
  $json['streamitem_id'] = $articles_info['streamitem_id'];
  $json['streamitem_content'] = $articles_info['streamitem_content'];
  $json['streamitem_timestamp'] = $articles_info['streamitem_timestamp'];
  // Don't do this!
  //mysqli_free_result($articles_data);
}
// If you need to, free it outside the loop
mysqli_free_result($articles_data);

我注意到您在调用 mysqli_fetch_array() 时未指定 MYSQLI_ASSOC,因此您将返回数字键和关联键。如果您使用 JSON 中的所有内容,并且使用 MYSQLI_ASSOCmysqli_fetch_assoc(),则无需执行所有这些分配:

while($articles_info = mysqli_fetch_assoc($articles_data)) {
  // No need for the $json array. Just use $articles_info directly
  // if you were going to json_encode() it.
}

关于php - 无法获取 Mysqli_result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12237683/

相关文章:

javascript - 在 CodeIgniter 的外部 JS 文件中复制 base_url

php password_verify() hash 和 pass 不匹配

php - 在php中编写sql代码并用php将变量写入文件

php - 我们应该始终跟踪查询和数据库错误吗?

php - 想要升级我用 mysql* 编写的数据库类。接下来我应该使用什么?

javascript - 表单在 php while 循环中呈现相同的数据

php - 如何向 Doctrine2 中的连接表添加额外的列?

PHP 替换数组中的数组值

php - 检查访问者IP是否在MySQL中

php - Laravel 的输入 :all() ignores disabled inputs