【问题标题】:Error can't fetch mysqli_result but fetches it anyways错误无法获取 mysqli_result 但无论如何都会获取它
【发布时间】:2013-08-09 18:00:23
【问题描述】:

所以我正在编写一小段代码,它会获取查询结果并将其打印出来,但它也给了我这个:

警告:mysqli_result::fetch_assoc():无法在第 24 行的 /var/www/vhosts/apexfunrun.com/httpdocs/dev/timer.php 中获取 mysqli_result

    $query = "SELECT field_pep_rally_date_value FROM dr_content_type_school WHERE nid = '$schoolname'";

if ($result = $mysqli->query($query)) {    
while ($row = $result->fetch_assoc()) {
    $date = $row['field_pep_rally_date_value'];
    $date = str_replace('-', '/', $date);
        echo date('Y-m-d', strtotime($date));
        $result->close();
    }
}
$mysqli->close();

【问题讨论】:

  • 也许你不应该调用 $result->close();在你的while循环里面?

标签: php mysql sql


【解决方案1】:

你在循环中有$result->close();。将它移到外面,如下所示:

if ($result = $mysqli->query($query)) {    
  while ($row = $result->fetch_assoc()) {
    $date = $row['field_pep_rally_date_value'];
    $date = str_replace('-', '/', $date);
    echo date('Y-m-d', strtotime($date));    
  }
  $result->close();
}

使用您拥有的代码,您肯定会在结果中看到第一个$date

请注意,实际上没有必要关闭这些资源,因为 PHP 会在脚本结束时释放它们。然而,手动指定它们是一个好习惯,但不要在循环中。

【讨论】:

  • 感谢您的快速回复!
猜你喜欢
  • 2018-03-26
  • 2012-08-27
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2018-12-09
相关资源
最近更新 更多