MySQL P_connect.... is this a temporary connection?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

MySQL P_connect.... is this a temporary connection?

Post by simonmlewis »

I have recently had a problem where a $result was not freed from some code, and as such there were too many concurrent connections.

The advice given has been to use p_connect as it will self-close after a short time anyway, meaning there will never be too many concurrent connections.

I can only find pconnect online, which is for a permanent connection. I assume that you put that in a toplevel index.php file, and just use the querys on the include pages.

While the temporary connection appeals to me, I wonder if the permanent connection is better, since the site running is wholely DB related.

Anyone got some good advice or wanna PM me to help.

Simon
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
akuji36
Forum Contributor
Posts: 190
Joined: Tue Oct 14, 2008 9:53 am
Location: Hartford, Connecticut

Re: MySQL P_connect.... is this a temporary connection?

Post by akuji36 »

This is sample code that will connect to a db and table:
and format your results:

<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM friends")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>";
}
Print "</table>";
?>

I have always preferred to use mysql close.

Follow this link:

http://www.php-mysql-tutorial.com/wikis ... abase.aspx
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: MySQL P_connect.... is this a temporary connection?

Post by simonmlewis »

Thanks, but why are you sending this?

I am after a temporary connection that will self close, which I am told is P_Connect.

What you have sent is the very basics of MySQL connection.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: MySQL P_connect.... is this a temporary connection?

Post by jackpf »

I believe p_connect is for a persistent connection. So if you call mysql_pconnect() multiple times, it'll still only connect to the database once.

However, I think PHP automatically disconnects from mysql after the script has finished parsing...so I don't really know if it will resolve your issue. Worth a shot though.

You could try using mysql_free_result(), and make sure you only call mysql_connect() once...that might help.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: MySQL P_connect.... is this a temporary connection?

Post by simonmlewis »

Mmmm.
The original reason for this query was that I caused a bit of an overload on a web server database, and it was because there were a few open connections that were not closed.

They suggested I use the temporary connection method, as it would self close and never have happened.

But couldn't find the script that did it. Still can't.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: MySQL P_connect.... is this a temporary connection?

Post by jackpf »

Hmm. Sounds like you need to optimise the query tbh.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: MySQL P_connect.... is this a temporary connection?

Post by simonmlewis »

Well not, it's nothing wrong in the query - it's the connection.
I need a temporary connect method, so that if the 'close' script is forgotten, the query will close on itself after a period of time.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: MySQL P_connect.... is this a temporary connection?

Post by jackpf »

But it does - the connection is closed at the end of the script.
php.net wrote:Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: MySQL P_connect.... is this a temporary connection?

Post by simonmlewis »

Only if you tell it to!
If you forget.... ie on a very long page.... then you come across these problems.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply