phpjayx Posted January 10, 2013 Share Posted January 10, 2013 I have a lot of functions in my PHP file doing database queries... but this one for some reason my debugger is saying it can't close the connection The debugger statement is the following. --------------------------- mysql_close(): supplied argument is not a valid MySQL-Link resource in <b>....etc, gives a path name and points to specific code line.... ---------------- Of course if I take out the close statement it doesn't choke, but I'm worried some queries are getting left open running on my server..... since things seem to be slowing down quickly. function changeCell($param) { $link = getConection(); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db('MyDatabase'); $sql_select_current_change = "select * from data where id=" . $param['dataid']; $query = mysql_query($sql_select_current_change); $result = mysql_fetch_array($query); mysql_close($con); $change_status = $result['ISCLICK_NUMBER2']; if ($change_status >0){ runNumbers(); } $change_status++; $sql_change_click = "UPDATE data set ISCLICK_NUMBER2='$change_status' where id=" . $param['dataid']; $result = mysql_query($sql_change_click); mysql_close($con); if ($result) return $param['dataid'] . '_' . $change_status; else return "false"; } Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 10, 2013 Share Posted January 10, 2013 A) You have a wrong variable name in your mysql_close statement, which is why it isn't a link resource. The error message probably told you it was a null/non-existent value. B) You should NOT be opening/closing database connections more than ONCE on any page. Each time you open a database connection, it takes a significant amount of time to contact the database server, authenticate the database username/password, and obtain a connection handle. You should open a connection once on any page that needs it, then either close it after you are done using it on that page, or more easily, let php close the connection when it automatically destroys all the resources used on any page. Quote Link to comment Share on other sites More sharing options...
phpjayx Posted January 11, 2013 Author Share Posted January 11, 2013 Thanks for the help. I hired someone to create the page for me, so it looks like there were some things not cleaned up. The last statement you made, about closing the connection only when PHP destroys all the resources used on any page.... Is this done automatically or do I need to put some command in to detect when the resources are destroyed, and then do the close? Thanks Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 11, 2013 Share Posted January 11, 2013 It's done automatically. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2013 Share Posted January 11, 2013 (edited) It's also kind of fortunate that the person who wrote that code managed to get the wrong variable name in the mysql_close statement, because if the variable name had been correct, the UPDATE query would fail because there would be no database connection at that point. In this case, having a programmer that didn't know what he was doing actually produced code that 'worked'. Edited January 11, 2013 by PFMaBiSmAd Quote Link to comment 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.