【问题标题】:mysqli_fetch_object(): Couldn't fetch mysqli_result in [closed]mysqli_fetch_object():无法在[关闭]中获取mysqli_result
【发布时间】:2014-06-26 19:24:45
【问题描述】:

这是我的代码:

<?php
$product_id = $_GET['id'];

if($connect = mysqli_connect('localhost','root','','accounting')){

    $sql = "SELECT * FROM inventory WHERE product_id='$product_id'";
    $query = mysqli_query($connect,$sql);
    mysqli_free_result($query);
    mysqli_close($connect);
}else{
print "Connection failed";
}

while ( $fetch = mysqli_fetch_object($query) ) {
    print $fetch->id;
}

?>

我不知道为什么我的代码不起作用!,它似乎是正确的。 帮帮我:)

【问题讨论】:

  • 帮帮我不会帮你找到答案...
  • 可能存在许多问题:凭据错误、查询语法错误、查询不存在的列、无效/不存在的“product_id”、未正确安装的 PHP,或者可能是事实上,没有像mysqli_free_result()这样的功能。如果没有更多上下文,这里没有人可能可能知道您的具体问题是什么。
  • 在查询后将var_dump($product_id); 放在$product_id = $_GET['id'];var_dump($sql); 下,看看有什么回声。另外,将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1); 看看是否会产生任何错误。
  • @Fred-ii- 不再阻塞此评论线程,但我put in a bug for this

标签: php mysqli


【解决方案1】:
mysqli_free_result($query);

这会丢弃结果,因此您在释放它之后 无法从中获取结果也就不足为奇了!

http://www.php.net/manual/en/mysqli-result.free.php 说:

当您的结果对象不再需要时,您应该始终使用 mysqli_free_result() 释放结果。

(强调我的)

将调用 mysqli_free_result() 移到 while 循环完成之后。

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 2011-12-22
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2013-11-14
    • 2022-08-05
    • 2013-05-07
    • 2014-08-07
    相关资源
    最近更新 更多