Page 1 of 1
mysql_connect and mysql_pconnect
Posted: Tue Aug 01, 2006 12:12 am
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.
Posted: Tue Aug 01, 2006 12:14 am
by Benjamin
You cannot close persistant connections with mysql_close();
Posted: Tue Aug 01, 2006 12:36 am
by sathish.hc
feyd | Please use 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
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]
Posted: Tue Aug 01, 2006 12:39 am
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
Posted: Tue Aug 01, 2006 12:40 am
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?
Posted: Tue Aug 01, 2006 12:41 am
by Benjamin
mysql_close() is probably unsetting $rs even though the db connection isn't getting closed.
Posted: Tue Aug 01, 2006 1:06 am
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
Posted: Tue Aug 01, 2006 1:28 am
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.
Posted: Tue Aug 01, 2006 1:38 am
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);
?>