This is not what I would consider a critical question but it has been bugging me a little.

If I have a number of mysql queries that I drop directly into an array because there is only a single result. Doing so uses a line of code such as this

$reservation['customer']=mysql_fetch_assoc(mysql_query("SELECT * FROM customers WHERE id='".$reservation['customerid']."' LIMIT 1"));

This is great and works well and is a single line which helps keep my code clean. HOWEVER, I am concerned that mysql might get bogged down over time if I do not clear the result using mysql_free_result()

Enter the question. It seems only way to perform a mysql_free_result on the query above to use something like this.

$result=mysql_query("SELECT * FROM customers WHERE id='".$reservation['customerid']."'");
$reservation['customer']=mysql_fetch_assoc($result);
mysql_free_result($result);

Is there a shorthand for this? It just seems like 3 lines of code where I should be able to do it in 1 or 2.

Recommended Answers

None that I can think, but then again, I personally don't stress over an extra line of code. Maybe if I thought I could turn 300 lines of code into 30, that would be something to work towards. 3 lines of code sounds pretty neat and clean to me.

How …

Jump to Post

All 4 Replies

None that I can think, but then again, I personally don't stress over an extra line of code. Maybe if I thought I could turn 300 lines of code into 30, that would be something to work towards. 3 lines of code sounds pretty neat and clean to me.

How many queries are you running that would require you to put into an array?

not that many. The most complex page load might have 7-8 single entry mysql queries. The real thing that I am noticing is that I can reduce my mysql load by using the mysql_free_result as much as possible.

I'm not gonna kill myself over needing an extra few lines of code but I figured it was worth the question.

Thanks.

Anytime you run a query that gives back results back from the database you will want to free it up using mysql_free_results. Think of it as just being necessary everytime.

Also, just thought I would let you know in case you didn't already, mysql function in PHP has been deprecated and you should be using mysqli or PDO now (if you're using a recent version of PHP that is).

This is where a function comes in. Create a function for those three lines, and all other times you need to use it, a single line suffices.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.