I was looking through my code and read that it was recommened to use mysqli_free_result when your result object is not needed anymore. But after seeing that each query is ou
It’s never strictly necessary, but it's good practice to keep an eye on resources you’re using and know when you don't need them anymore.
It’s pretty minimal effort to drop in that extra line of code, so I would just do it every time you're finished with a result set. It has the added bonus of making it clear to someone reading your code when you’ve finished with a resource.
It is cluttering the code. This is not a functionality-based reason to drop mysqli_free_result
, but in my opinion a valid reason. Consider the snippet below.
You will not save a lot of memory freeing fetch-a-single record queries. Skip them unless you know that they are necessary - when some script uses more memory than you can allocate (should be rarely).
Another reason to free these resources would be an OS-level restriction on the amount of open file handles. However, I think all queries go through this one connection or one filehandle or socket.
// Share PDF
if ($_REQUEST['do'] == 'sharePDF') {
- $sql = "SELECT * FROM c_document WHERE doc_id = $doc_id";
- $res = mysql_query($sql);
- $doc_row = mysql_fetch_assoc($res);
- mysql_free_result($res);
- $d_name = $doc_row['d_name'];
- $templ_id = $doc_row['templ_id'];
- $sql = "SELECT * FROM c_template WHERE templ_id = $templ_id";
- $res = mysql_query($sql);
- $templ_row = mysql_fetch_assoc($res);
- mysql_free_result($res);
+
+ $doc_row = qAssocOne("SELECT * FROM c_document WHERE doc_id = $doc_id");
+ $templ_row = qAssocOne("SELECT * FROM c_template WHERE templ_id = {$doc_row['templ_id']}") ;
+
$params = array(
'share_pdf_core_id='.$templ_row['share_pdf_core_id'],
'share_pdf_create_id='.$templ_row['share_pdf_create_id'],
@@ -193,7 +187,7 @@ if ($_REQUEST['do'] == 'sharePDF') {
'doc_id='.intval($_REQUEST['doc_id']),
'pdf1='.urlencode($_REQUEST['pdf']),
'pdf2=',
- 'name='.urlencode($d_name),
+ 'name='.urlencode($doc_row['d_name']),
'select_output='.$templ_row['select_output']
);
$params = join("&",$params);
The subtracted lines are absolute garbage in my opinion; ten lines vs two. Please...
In response to the comment from Your Common Sense:
No one should care what the function does. It illustrates a point.
Further, the name gives you hints. Q
for query, Assoc
for fetch (hydrate) a hash as opposed to numeric array. One
for one record and not a set of records. If you really can not guess what it does, search online. The code is available. First hit on google.
It is not unrelated. A lot of PHP code looks like this. My point is stated in the answer. It is unnecessary because clearing a result set with one row will not save you much memory. It is unnecessary because it clutters the code and obfuscates the intent or business logic.
have-nice-day
Actually it’s necessary, because it might make a heavy load into the server when many requests are made. So preferably, you should use it.
Some other cases when you know that this query is followed by other query so you don’t have to use it.