Dfference mysql_connect and mysql_pconnect

If you’re connecting to a MySQL database in your PHP application, you’ll find that there are two alternative connections – mysql_connect which establishes a new connection “each time” and mysql_pconnect which uses persistent connections. Which should you use?

1: 

Mysql_connect:
opens a new connection each time a PHP page is called up, and closes the connection down again at the end of the request. It’s ideal for pages that don’t have a heavy usage – doesn’t need tuning, is straightforward internally. If you compare MySQL to a shop, this is the connection that you would use for a small shop where the door is opened each time a new customer wishes to enter.

Mysql_pconnect:
Mysql_pconnect will also open a new connection when a PHP page is called up (at any rate, it will the first time after a server reboot), but it will NOT close the connection at the end of the request – instead, it will save it in a connection pool so that a subsequent request can continue to use the same connection. It’s intended for pages that do have a heavy usage – where the resources burn up by opening and closing connections every time might have a severe effect on performance. If your local supermarket had a door that was opened each time someone went in and out, there would be a lot of needless opening and closing going on – better to leave it open and let a whole lot of people in and out at the same time.

2:
mysql_connect() and mysql_pconnect() both are working for database connection but with little difference. In mysql_pconnect(), ‘p’ stands for persistance connection.

When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .

But in case of mysql_pconnect() function,

First, when connecting, the function would try to find a (persistent) connection that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).

mysql_pconncet() is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.

Leave a Reply

Your email address will not be published. Required fields are marked *