I am currently opening a database connection in my application initialization. This is a fairly small application, PHP, if relevant.
Should I connect to the database, make calls, and then close and repeat this process for every database function that I write?
For example, I have the following function that grabs the $ db variable from my application initialization.
function get_all_sections() { global $db; $sql = 'select * from sections'; if (!$db->executeSQL($sql, $result)) { throw new Exception($db->getDatabaseError()); exit(); } $sections = array(); for ($i = 0; $i < $db->numberOfRows($result); $i++) { $sections[] = new Section($db->fetchArray($result, MYSQLI_ASSOC)); } return $sections; }
Would it be better if I opened the connection and then closed it after I took the lines? This is like a lot of connections that open and close.
optimization database php mysql
mculp
source share