We're not part of the solution

How to fix Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource

Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in… blah blah blah.

If you need to use  mysql_free_result(), you may have received this error. What causes it and how can you fix it?

First, what is mysql_free_result()? When you make mySQL database queries that return large sets of data, it can cause your server to bog down because those results are hogging up a lot of memory. On a high-traffic site, it can actually crash your server as more and more of those results build up. So, mysql_free_result() is a built-in PHP function that releases the results of your query from memory. Again, this is usually not a big deal unless you are returning large data sets from your queries, but it is always good practice to clear your results and free up the memory on your server. Here is an example of what the PHP code might look like:


//...CONNECT TO DATABASE...

$query = "SELECT * FROM employees WHERE age >= 30";
if ($result = mysql_query($query)) {
	while ($row = mysql_fetch_array($result)) {		
		//...DO SOMETHING HERE...
	}
	mysql_free_result($result); //free the query results from memory
}

///...CLOSE YOUR DATABSE CONNECTION...

But, if you have strict PHP warnings enabled on your server, you may start getting the warning above for some (but not all) of your queries. I had a really hard time tracking this problem down, but I finally found it here. You only use mysql_free_result() for the following query types: SELECT, SHOW, EXPLAIN, and DESCRIBE. So, if you perform an UPDATE query, it doesn’t return a valid resource and throws the warning when you call mysql_free_result(). Duh.

Previous

Flash AS3: A quick test of Five3D

Next

Why HTML5 is not going to kill Flash anytime soon.

5 Comments

  1. Michael Bullard

    Another common cause of this error message is if you have a PHP include file that has a Recordset with the same name as a Recordset in the main page. Easy solution: rename one of the Recordsets so they don’t conflict. Gets me every time. Duh again.

  2. Hi, guys excellent solutions that’s help me so much (sorry my English , ha ha)

  3. Zoveum

    @Michael Bullard

    Thanks for the hint, I´m getting this error message because on recordset with the same name.

  4. Thank you ,my server and I are exhausted by this problem when I deal with 10000000 queries and more .

  5. Thanks a lot by the explanation. Worked!

Powered by WordPress & Theme by Anders Norén