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?
The MySQL Clients send connection requests to the MySQL Server. A connection request is simply a TCP-IP connect message sent to port 3306 on the server host machine. Receiver Thread. Incoming connection requests are queued and then processed by the receiver thread one by one.
PHP Data Objects (PDO) is an extension that serves as an interface for connecting to databases. Unlike MySQLi, it can perform any database functions and is not limited to MySQL. It allows flexibility among databases and is more general than MySQL. PDO supports both server and client-side prepared statements.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With