![]() ![]() However, PHP, Apache/IIS/whatever, have lives of their own and they are capable of using the connections you open beyond the life of your script. the database) to, efficiently and effectively, is the entirety of a single script. What you first of all are wanting to apply resources (i.e. The basic unit of execution presumably is an entire script. I might not be 100% correct here but I believe the odds are in my favour. Then your 'disconnect' function will call mysql_close() which means PHP A) checks that mysql_close() exists and B) that you have provided all needed arguments to mysql_close() and C) that they are the correct type (mysql resource).It also has to create a copy of the $connection variable in the new local scope. PHP has to check that your function A) exists, B) is not private/protected and C) that you provided enough arguments to your function. You have to call your 'disconnect' function, and function calls are one of the more expensive operations in PHP. ![]() This means PHP has to check that the variable $_connected A) exists B) is a boolean and C) is true/false. You have to check the value of $_connected for every single query.PHP engine calls mysql_close() internally for each connection.PHP engine loops through internal array of mysql connections.The cost of checking $_connected before each query combined with the cost of actually calling $db->disconnectFromDB() to do the closing will end up being more expensive than just letting PHP close the connection when it is finished with each page.ġ: If you leave the connection open till the end of the script: (I was not the one that wrote the text above)ĭon't bother disconnecting. If PHP and MySQL are on the same server or local network, theĪpache does not work well with persistent connections. Normally temporary tables are dropped when the connection closes, When you lock a table, normally it is unlocked when the connection I just read this comment on PHP website regarding persistent connection and it might be interesting to know: What advantages/disadvantages do either have? What has worked best in your situation? Is there anything really wrong with forgetting to close the mySQL connection, besides a small loss of performance? Or should I just include that line above at the very end of the page? Now if it was closing after every query, would this slow it down a lot as opposed to me just adding this line to the end $db->disconnectFromDB() $teachers = $db->query('SELECT id FROM teachers') because I was thinking something like (code is an example only) $students = $db->query('SELECT id FROM students') Should this be called after every query (as a private function) or externally via the object. Now I have a method in my DB class called disconnectFromDB() which pretty much calls mysql_close() and sets $_connected = FALSE (so the query() method will know to connect to the DB again). This basically means that it doesn't call mysql_connect() until the first query is handed to it, and it subsequently skips reconnecting from then on after. I use lazy connection to connect to my DB within my DB object. ![]()
0 Comments
Leave a Reply. |