【问题标题】:Couldn't fetch Mysqli_result无法获取 Mysqli_result
【发布时间】:2012-08-27 14:09:51
【问题描述】:

我遇到了这个错误

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

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

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

【问题讨论】:

  • 希望$lastID 被正确过滤/清理/转义。否则你很容易受到SQL injection attacks
  • 是的,使用 msqli_real_escape_string($last_id)。为什么人们对这里的 SQL 注入如此着迷?
  • @dave 因为我们看到了很多很多真正糟糕的 PHP 代码,并且遇到了很多甚至从未听说过 SQL 注入的新用户 :( 安全总比抱歉好,所以我们如果我们在发布的代码中没有明确看到转义和过滤,总是指出来。
  • @dave:我的粗略估计是这里大约 90% 以上的 php 发帖者对注入问题一无所知。
  • 很好@MichaelBerkowski,也许我会开始在网站周围做同样的事情来帮助别人。

标签: php mysqli


【解决方案1】:

马上,您似乎在 fetch 循环中调用了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_ASSOC 的情况下调用了 mysqli_fetch_array(),因此您同时获得了数字键和关联键。如果您使用 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.
}

【讨论】:

    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多