Page 1 of 1
MySQL P_connect.... is this a temporary connection?
Posted: Fri Aug 07, 2009 8:36 am
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
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 12:11 pm
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
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 12:19 pm
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.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 12:24 pm
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.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 12:29 pm
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.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 12:33 pm
by jackpf
Hmm. Sounds like you need to optimise the query tbh.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 1:05 pm
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.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 1:08 pm
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.
Re: MySQL P_connect.... is this a temporary connection?
Posted: Mon Aug 24, 2009 1:14 pm
by simonmlewis
Only if you tell it to!
If you forget.... ie on a very long page.... then you come across these problems.