Reputation: 17348
Whenever a query finishes executing, I know you should free the result. Here is a snippet of a class I built for running a simple query. Could someone tell me where I went wrong? The query, when entered properly, runs successfully. It's just that my page doesn't reload like it, should but it gives me these errors...
Fatal error: Call to a member function free() on a non-object in <file> on <line>
... and this error ...
Fatal error: Call to a member function close() on a non-object in <file> on <line>
Here a simplified version of my code:
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());
}
}
Upvotes: 0
Views: 1276
Reputation: 131871
If I follow your link to the manual, there is something mentioned about the return value
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.
Especially you don't handle the two cases true
and more important 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");
}
I don't know, what $query
is in your case, but query()
will only return a result, if there is one (SELECT
-query).
Upvotes: 5