每当查询完成执行时,我知道您应该释放结果。这是我为运行简单查询而构建的类的片段。有人可以告诉我哪里出错了吗?正确输入后,查询将成功运行。只是我的页面没有像它应该那样重新加载,但它给了我这些错误...
Fatal error: Call to a member function free() on a non-object in <file> on <line>
... 这个错误...
Fatal error: Call to a member function close() on a non-object in <file> on <line>
这里是我的代码的简化版本:
public function query($query) {
try {
if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
$result->free(); //Problems here
$result->close(); //Problems here
return $result;
} else {
$result->free(); //Problems here
$result->close(); //Problems here
throw new Exception("The query was invalid");
}
} catch (Exception $e) {
die($e->getMessage());
}
}
最佳答案
如果我点击您的手册链接,会提到有关返回值的内容
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.
特别是你不处理这两种情况 true
和更重要的 false
if ($result = $this->connection->query($query, MYSQLI_USE_RESULT)) {
$result->free(); // Probably call "free()" on true
$result->close();
return $result;
} else { // <-- false, null, 0 or ""
$result->free(); // Probably call "free()" on false
$result->close();
throw new Exception("The query was invalid");
}
我不知道你的情况下 $query
是什么,但是 query()
只会返回一个结果,if是一个(SELECT
-查询)。
关于php - MySQLi PHP 扩展问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6377668/