Jump to content

Recommended Posts

So I just started using mysql_free_result on all my methods in a certain little class I've been working on and for some reason I keep getting an error when I use it on an UPDATE query. Here's the method in it's entirety:

 

public static function update_views($photo_id, $views, $redirect = NULL) {
	$views		= $views + 1;
	$query 		= "UPDATE " . PHOTOS_TABLE_PHOTOS . " SET views = '" . $views . "' WHERE id = '" . $photo_id . "'";
	$result 	= mysql_query($query);

	mysql_free_result ($result);

	$redirect	= isset ($redirect) ? header ("Location: " . $redirect) : NULL;
}

 

Am I doing something wrong here? I call the function after the query has been executed, but I'm getting this stupid error:

 

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in C:\Program Files\wamp\www\index.php on line 1307

 

Where line 1307 is where this method is being called. I'm confused?

Link to comment
https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/
Share on other sites

Only SELECT and SHOW (EXPLAIN) queries return result resources. INSERT/UPDATE queries only return boolean TRUE/FALSE and there is nothing for a free_result statement to free up.

 

Edit: As stated in the documentation for the function you are trying to use -

 

If a non-resource is used for the result, an error of level E_WARNING will be emitted. It's worth noting that mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.

 

 

Only SELECT and SHOW (EXPLAIN) queries return result resources. INSERT/UPDATE queries only return boolean TRUE/FALSE and there is nothing for a free_result statement to free up.

 

Edit: As stated in the documentation for the function you are trying to use -

 

If a non-resource is used for the result, an error of level E_WARNING will be emitted. It's worth noting that mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.

ah, overlooked that.

OP, update queries effect only one row, unless looped. So you really shouldn't be worried about memory usage here. The memory is automatically freed at the end of execution.

Only SELECT and SHOW (EXPLAIN) queries return result resources. INSERT/UPDATE queries only return boolean TRUE/FALSE and there is nothing for a free_result statement to free up.

 

Edit: As stated in the documentation for the function you are trying to use -

 

If a non-resource is used for the result, an error of level E_WARNING will be emitted. It's worth noting that mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.

 

Oh wow. I saw that it said boolean was returned and not a resource. Should've realized haha. Thanks for the tip!

Here's a different slant on the problem - variables created inside functions and class methods are local to that invocation of the function/class method and are destroyed when that function/class method call ends (unless you declare the variable static inside that function/class method) and there's no need for you to be freeing up resources inside functions/class methods unless you have complicated code in that function/class method that executes more than one query that returns large result sets.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.