php 无法获取 Mysqli_result
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12237683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Couldn't fetch Mysqli_result
提问by dave
I've got this error
我有这个错误
Warning: mysqli_fetch_array() [function.mysqli-fetch-array]: Couldn't fetch mysqli_result in /home/fights7/public_html/include/load_more_home_posts.phpon line 12
警告:mysqli_fetch_array()[function.mysqli取阵列]:在无法提取mysqli_result /home/fights7/public_html/include/load_more_home_posts.php上线12
And would like to know what I've done wrong with the below code?
并想知道我在下面的代码中做错了什么?
$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);
回答by Michael Berkowski
Straight away, it appears that you are calling mysqli_free_result()inside your fetch loop, so after the first loop iteration, your result resource has been closed and freed, and no more results will be available.
马上,您似乎是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);
I note that you're calling mysqli_fetch_array()without specifying MYSQLI_ASSOC, and so you're getting both numeric and associative keys back. If you are using everything in your JSON, you don't need to do all those assignments if you use MYSQLI_ASSOCor mysqli_fetch_assoc():
我注意到您在调用时mysqli_fetch_array()没有指定MYSQLI_ASSOC,因此您同时获得了数字键和关联键。如果您在 JSON 中使用所有内容,则在使用MYSQLI_ASSOCor 时无需执行所有这些分配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.
}

