jamesjmann Posted December 30, 2011 Share Posted December 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/ Share on other sites More sharing options...
AyKay47 Posted December 30, 2011 Share Posted December 30, 2011 the error is telling you that something is invalid in your query itself.. debug it using die() and mysql_error() $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/#findComment-1302528 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/#findComment-1302531 Share on other sites More sharing options...
AyKay47 Posted December 30, 2011 Share Posted December 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/#findComment-1302539 Share on other sites More sharing options...
jamesjmann Posted December 30, 2011 Author Share Posted December 30, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/#findComment-1302540 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2011 Share Posted December 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/254077-mysql_free_result-not-working/#findComment-1302543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.