mysql_connect and mysql_pconnect

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
sathish.hc
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 2:55 am

mysql_connect and mysql_pconnect

Post by sathish.hc »

Hi all,

I gone thru tutorials and manual of mysql_pconnect. It says the following

mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link 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 link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

This type of link is therefore called 'persistent'.


But when I use mysql_pconnect to establish connection, mysql_close() will work. I am really confused about mysql_pconnect can any one clarify this

feyd | you don't need to bold large portions of your post.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You cannot close persistant connections with mysql_close();
sathish.hc
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 2:55 am

Post by sathish.hc »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Following is my code

Code: Select all

<?

$rs = mysql_pconnect("localhost", "root", "root");
mysql_select_db("test");
$query = "select * from emp";

mysql_close($rs);
$result = mysql_query($query);

while($arry = mysql_fetch_array($result)){
	print_r($arry);
    echo "<br>";
}

?>
If I execute this I am getting following error.

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\testing\ptest.php on line 10

If I remove mysql_close() from the code I works fine.

Then what is meaning of 'persistent' over here


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

The handy PHP manual wrote: Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).
http://www.php.net/mysql_pconnect
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Right out of the manual:
"mysql_close() will not close links established by mysql_pconnect()"

So there must be some other issue or something.

Edit: what happens when you print_r() the $rs variable? can you do that and post the result?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

mysql_close() is probably unsetting $rs even though the db connection isn't getting closed.
sathish.hc
Forum Newbie
Posts: 15
Joined: Tue May 23, 2006 2:55 am

Post by sathish.hc »

If I print $rs variable It gives the following output
Resource id #1.

But this is same for both mysql_connect and mysql_pconnect
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I think you are misunderstanding persistent to your script vs persistent to PHP. If you use the connect() functions, when you do a close() PHP will close its connection to the database server. If you use the pconnect() functions, when you do a close() PHP will NOT close its connection to the database server and will maintain it in a connetion pool. So when you reconnect after a connect() you will get a new connection. When you reconnect after a pconnect() you will get an existing connection from the pool if a free one is available.

None of this has anything to do with the links you get in your script which will appear the same no matter which type of connection is used.

Finally, I believe the consensus is to use the connect() functions until you run into a specific performance problem that the pconnect() functions can help resolve.
(#10850)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Never issue a close command until you are finished retrieving data from the database server. When you issue the close all result sets are purged and the connection ID is deleted.

If you are using persistent connection the connection ID is still deleted but the connection is still there waiting for another query connection to be initiated. You will have to issue another query.

In otherwords never use the mysql_close until you are completely finished interacting with mysql.

This would be the proper way...

Code: Select all

<?php

$rs = mysql_pconnect("localhost", "root", "root"); 
mysql_select_db("test"); 
$query = "select * from emp"; 

$result = mysql_query($query); 

while($arry = mysql_fetch_array($result)){ 
print_r($arry); 
echo "<br>"; 
} 

mysql_close($rs); 
?>
Post Reply