Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does database connection works in php-mysql

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?

like image 251
Imrul Avatar asked Sep 09 '09 11:09

Imrul


People also ask

How does MySQL connection work?

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.

What is PHP explain database connection in PHP?

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.


1 Answers

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.

like image 130
Gumbo Avatar answered Sep 29 '22 21:09

Gumbo