mysql_connect and mysql_pconnect
Moderator: General Moderators
-
sathish.hc
- Forum Newbie
- Posts: 15
- Joined: Tue May 23, 2006 2:55 am
mysql_connect and mysql_pconnect
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.
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.
-
sathish.hc
- Forum Newbie
- Posts: 15
- Joined: Tue May 23, 2006 2:55 am
feyd | Please use
If I execute this I am getting following error.
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]
Following is my codeCode: 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>";
}
?>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 10If 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]http://www.php.net/mysql_pconnectThe 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()).
-
sathish.hc
- Forum Newbie
- Posts: 15
- Joined: Tue May 23, 2006 2:55 am
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.
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)
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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...
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);
?>