6

我有这个错误

警告:mysqli_fetch_array() [function.mysqli-fetch-array]:无法在第12行的 /home/fights7/public_html/include/load_more_home_posts.php 中获取mysqli_result

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

$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);
4

1 回答 1

16

马上,您似乎mysqli_free_result()在 fetch 循环内部调用,因此在第一次循环迭代之后,您的结果资源已被关闭并释放,并且不再有可用的结果。

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,所以你得到了数字键和关联键。MYSQLI_ASSOC如果您使用 JSON 中的所有内容,则使用or时不需要执行所有这些任务mysqli_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.
}
于 2012-09-02T16:27:25.713 回答