When shall I use
mysql_connect(DB_HOST,DB_USER,DB_PASS)
and when
mysql_pconnect(DB_HOST,DB_USER,DB_PASS)
What is the difference
When shall I use
mysql_connect(DB_HOST,DB_USER,DB_PASS)
and when
mysql_pconnect(DB_HOST,DB_USER,DB_PASS)
What is the difference
when you use mysql_connect(), each time someone asks for the page that makes the call, php establishes a new connection to the database. When php finishes sending the page to the user, it closes the connection to the database.
But when you use mysql_pconnect(), the first time someone asks for the page, php establishes the connection, and it won't close this connection after the page is sent. When someone else asks for that page, php will reuse the connection it established before.
so, if you have 100 requests for the page in question, php will create and destroy 100 connections to the database for the first case (mysql_connect()), but it will work with just 1 connection using the mysql_pconnect() function... big difference, isn't it?
This is just theorical, because the fact is that php will establish one persistent connection for each httpd child that receives the request for the page.
So, if you have 10 httpd childs, you're system will attend the 100 requests with only 10 persistent connections, which still is good.
Do not use persistent connections unless you know exactly what you're doing, are certain that you're not on a shared host, have only a small number of MySQL login identities that are used by your PHP scripts, and have plenty of RAM.
Some times you need to open a connection to the database and keep it live throughout and sometimes need to close it immediately. connect needs to be opened on every page and pconnect not untill it is closed.
Regards
T. A. Barry
Get FREE domain with every hosting.
So many features just for $79.99 per yr.
www.BestPriceHost.com
Can you give an example of when it would make sense to keep a database connection open while using something stateless like HTTP?
Hope the following URL replies your question.
http://www.phpbuilder.com/forum/archives/1/2001/12/3/129951
Regards
T. A. Barry
Get FREE domain with every hosting.
So many features just for $79.99 per yr.
www.BestPriceHost.com