How does database connection works in php-mysql
database, database-connection, mysql, php
Solution
Open the connection just once. Opening and closing a connection takes time too. And as you already said, PHP closes open connections at the end of the runtime automatically.
So just call `mysql_connect` whenever you need a connection and let PHP close it at the end. `mysql_connect` checks for already existing connections so you don’t need to worry that calling `mysql_connect` with the same parameters will open a new connection every time. You can also use persistent connections that can be used for more than just one script execution.
Problem
Is it better to close the connection after each query is executed or put the connection as is it, then php will automatically close that connection. Which one is better and why?