pernest Posted October 4, 2009 Share Posted October 4, 2009 Hi I'm using the latest WAMP. A php script I wrote was working well for several hours, but then hung. I have determined that it was the mySQL server which had gone tits up, and the script was waiting for a response. In my php script I am making millions of queries to a database, which is opened with a persistent connection. The query which is running is of the form $result = mysql_query("SELECT blah blah blah"); I understand that because I'm using SELECT, then $result will be a resource. If my script ended then I know that $result would be cleaned up properly. My question is: When the above line of code is executed for a second time, does the previous resource associated with $result get cleaned up properly before the new resource is assigned to it? Do I need to include the line mysql_free_result($result); after I have finished with the data in the resource $resultand before I make another query. Many thanks Paul I'm quite new to php and mysql, but not to programming. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/ Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Don't use persistent connection with ext/mysql. Use plain mysql_connect. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930199 Share on other sites More sharing options...
pernest Posted October 4, 2009 Author Share Posted October 4, 2009 Don't use persistent connection with ext/mysql. Use plain mysql_connect. Could you explain why? I don't understand what you mean by ext/mysql. Even if I use connect instead of pconnect, the connection will still be established for the lifetime of the script, and for the multiple calls queries. Could you explain why the difference between pconnect and connect is relevant to my question. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930202 Share on other sites More sharing options...
Alex Posted October 4, 2009 Share Posted October 4, 2009 Don't use persistent connection with ext/mysql. Use plain mysql_connect. Could you explain why? I don't understand what you mean by ext/mysql. Even if I use connect instead of pconnect, the connection will still be established for the lifetime of the script, and for the multiple calls queries. Could you explain why the difference between pconnect and connect is relevant to my question. Thanks with mysql_pconnect() the connection will not end when the script finishes. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930208 Share on other sites More sharing options...
The Little Guy Posted October 4, 2009 Share Posted October 4, 2009 mysql_free_result is only need if you have a query that returns a HUGE result set, otherwise it will be cleaned up. I currently have a script that has been running for a few days (non-stop), and all has been going well! Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930211 Share on other sites More sharing options...
pernest Posted October 4, 2009 Author Share Posted October 4, 2009 Sorry, but I think I must have included too much background information in my original post. If I have the following code: $result = mysql_query("SELECT product from products where checked=0 limit 0,1"); //do somthing with the result, and set the product to checked $result = mysql_query("SELECT product from products where checked=0 limit 0,1"); Does the second of assignment of $result clear up the resorce associated with the first assignment? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930218 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 It does. The variable gets overwritten with new data. This has nothing to do with MySQL server though. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930230 Share on other sites More sharing options...
pernest Posted October 4, 2009 Author Share Posted October 4, 2009 It does. The variable gets overwritten with new data. This has nothing to do with MySQL server though. but I asked, "Does the second assignment of $result clear up the resource associated with the first assignment?" I understand that the variable gets overwritten, but does the resource associated with the variable get properly cleaned by simply overwriting the variable? Or do I need to do the garbage collection myself before the reassignment? Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930254 Share on other sites More sharing options...
Daniel0 Posted October 4, 2009 Share Posted October 4, 2009 PHP's garbage collection should be able to figure out that you are no longer using the resource when the variable is overwritten. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930256 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 If you want to be sure call unset on the variable before reassigning it. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930260 Share on other sites More sharing options...
pernest Posted October 4, 2009 Author Share Posted October 4, 2009 Thanks so far, I'm still trying to understand what is happening. the line of code $result = mysql_query("SELECT product from products where checked=0 limit 0,1"); assigns a resource to $result. Will the resource referred to by $result be in the memory associated with the php script that has executed the command, or in the memory associated with the mysql daemon? Also where can I find the source code for the function mysql_free_result() so I can see what exactly its trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930277 Share on other sites More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 Once it's stored in $result variable it's in PHP memory. The source is in /ext/mysql/php_mysql.c Quote Link to comment https://forums.phpfreaks.com/topic/176470-solved-do-i-need-to-be-using-mysql_free_result/#findComment-930284 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.