mysql_pconnect() help...

michaelsanford

Translator, Web Developer
I've got a script that makes multiple accesses to a single dba. The script takes a form and checks a database for one of the elements in the form (it must be unique) and if it's already there re-displays the form. once all the verifications are complete it inserts a new record.

What are the advantages to using mysql_pconnect() over mysql_connect() ? Also, if I open a connection with mysql_pconnect() and the user takes a REALLY long time to correct data and resubmit the form, could the connection time out? How can I find out what the default connection timeout duration is?

Most importantly, what happens if I issue mysql_pconnect() twice? That is, once in the unique element verification, and then again in the dba insert routine? Will it create multiple connections and clog everything, or will it detect that I have already made a connection and use that one? (everywhere in ym script I use the optional MySQL connection pointer assigned by $connectionID = mysql_pconnect("", "", ""), so I presume if it does use the existing connection, it will assign my link pointer to the old connection?) I use the link pointer in places like $result = mysql_query("SELECT * FROM users", $connectionID);

I know that's convoluted, but thanks! :cool:
 
The main advantage is that the connection to the database doesn't close at the end of every script. Otherwise you have the right idea.
 
The "p" is for "persistent" wich is semi-true. It will keep the connection open for a while, but only if you are running in a script; it doesn't create a persistent connection if used as a cgi. That's because a new instance of PHP is launched for every call, and when the script is finished that instance is killed, along with all connections.

As far as I recall, every time "pconnect" is used by the same Apache process, PHP checks to see if there is a persistent connection open to the database for that process, and if there is, it will use it.

Also, you need to be careful about the number of connections, usually mySQL has a lower maximum setting than the web server. I beleive (though this might have changed) that mySQL has a default setting of 100, and Apache allows 150. Match the values of MySQLs "max_connections" parameter and Apaches "MaxClients" and you shouldn't have any problems.
 
Thanks elander, I know p is for persistent :p You answered my question about connecting twice; I presume you mean that PHP will just use an existing connection (paragraph 2) if there is already one available. So if I tried to issue mysql_pconnect() a second time with a previously established connection there would be no problems.

And I'll have a look in my PHP configs too, thanks for the tip!
 
Back
Top