mysql_pconnect or mysql_connect ?
I hope someone can answer these questions.
I've done some research and decided to use mysql_pconnect for the following reasons.
I've written my own CMS using objects in PHP. All the objects are in one file along with the connection function. (Please advise if seperate files are recommended). The objects file are included in most pages. Most functions within objects connect to the database but do not close the connection. Normally, I'd think this is a bad thing but if mysql_pconnect is being used, do I have to close the connect in each function?
Please correct me if I'm wrong; the mysql_pconnect simply looks for an existing connection and uses the same one virtually using only one connection.
Any help would be much apprecaited.
You might want to read this PHP manual page: http://www.php.net/manual/en/feature...onnections.php
Whether or not you opt to use persistent connections, I would not recommend having each class that needs DB access doing its own connection. Instead, I would have one database connection class, instantiate it to create the connection, then pass that object to each other object's constructor that needs it. If you use MySQLi instead of the older MySQL extension, you can just instantiate a MySQLi object and pass that to each applicable object.
public function __construct(MySQLi $db)
$this->db = $db;
public function bar($id)
$sql = "SELECT * FROM table WHERE id=" . (int) $id;
$result = $this->db->query($sql);
$db = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
$foo = new Foo($db);
$data = $foo->bar(1);
Last edited by NogDog; 10-01-2010 at 02:50 PM.
Thanks for responding NogDog.
That is what I've done. Maybe an example would help explain.
I created a connection class that accepts values in order to make the connection.
Then I've created other classes with function to actually do the work.
// database connection
mysql_pconnect("$host","$name","$password") or die(mysql_error());
As you can see the connection has been made ( providing the values are sent to the variables) and the function doesn't have a mysql_close() within it.
//load selected blog title into editor
$qry_edit_title="SELECT * FROM blog WHERE blog_title='$pg'";
// should there be a close connection here?
Imagine many functions in the same manner with not mysql_close(). Would this cause an issue and should I be closing the database within each function?
No, you do not want to close the connection as part of the query function, as any subsequent queries would then fail unless you then did another mysql_[p]connect() first. The persistent aspect of the pconnect only affects subsequent calls to mysql_pconnect(), not calls to any other mysql_*() function. In fact, if you do close it, then there is no reason to use mysql_pconnect(), as a closed connection is a closed connection, regardless of whether it was persistent.
PS: I've generally avoided persistent connections, as they can become problematic if anything is not configured just right, with orphaned connections using up your allowed limit of connections. They can make a performance difference in some cases, and you can always implement them later if you determine you need to find some performance boosts and are willing to keep an eye on things and tweak web server and database server settings to address any problems that arise. Generally, application design and database/query optimization will do much more to improve performance than using mysql_pconnect() once per script instead of mysql_connect() once per script.
Thanks for all your help
Thanks NogDog for all your help. I appreciate your time.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)