When there is more than one query in the same file it shows errors, I solve everything using$stmt->close();
I was presented with the doubt of which is the correct way of its use, how many close()
should be used.
The $stmt->free_result()
fulfills the same function if it is used, it should no longer be used close()
or if it is used close()
it should no longer be usedfree_result()
Can you explain me?
if ($stmt->fetch()) {
echo "Resultados";
} $stmt->close();
else {
$stmt->close();
echo "No existe resultados";
}
Y
while ($stmt->fetch()) {
echo "Resultados";
}
$stmt->close();
else {
$stmt->close();
echo "No existe resultados";
}
None of the methods you function belong to PDOStatement ( list of PDOStatement methods ) however if you mean the method
PDOStatement::closeCursor
and the functionmysql_free_result
.Both release memory but the first is when you use PDO and the other when you use the mysql api which is deprecated.
PDOStatement::closeCursor
you should use it when a query doesn't loop through the entire result and you want to execute another query, when the query is very large and you want to free up the memory used, when you want to reuse thePDOStatement
.The examples you put are not valid since the structure of the if and while is incorrect.
The ideal would be:
Y
Always use closeCursor, it never hurts. and those would be the ways that I recommend.
Let us first explain the difference between the two:
$stmt->free_result()
frees memory related to a result set, while$stmt->close()
freeing memory related to a prepared query. Subsequently, calling$stmt->close()
will cancel any remaining results.Basically, calling will
$stmt->close()
provide the same effect as calling$stmt->free_result()
since it also cancels the result set. But calling$stmt->free_result()
will not clear the memory used by the prepared statement, in which case you should use$stmt->close()
.As for which one to use, there may be situations where you intend to use the prepared statement you've initialized, but no longer need the result set you currently have. In that case, you would call
$stmt->close()
when you're done with the prepared statement (you can pass different values to the same prepared query... that's one of its advantages), and instead call$stmt->free_result()
before executing another statement.Specifically answering your question
In the code example that you put, if you look at the PHP Manual , the code is written like this:
What this code does is release the
$stmt
when exiting the loop. When closing$stmt
it is not necessary to use free.You also put an example of code using
if
... but withfetch
the data must be read within a loop, since when reading them within a simpleif
you will not be able to obtain all the results, in case there is more than one row.In short: If you use
close
when finished using your prepared query and data, it would be enough. If you want to usefree_result
beforeclose
, use it. There are debates about whether or not its use is vital. According to this answer the use offree_result
does not contribute anything. You can do a test withmemory_get_usage()
in various scenarios and determine if it really contributes something. According to the same answer, its use is not vital.