Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better: mysql_connect or mysql_pconnect

Tags:

php

mysql

I'm a PHP newbie working a some scripts to display some news articles from a databse and wanted to find out a couple of things.

  • For opening a connection to a MySQL database, which is a better option mysql_connect or mysql_pconnect?
  • What are the advantages or drawbacks of using a persistent connection to the database?
  • And in what kind of scenario will a persistent connection be called for?
like image 668
Kwame Avatar asked Aug 27 '09 12:08

Kwame


People also ask

What is the function Mysql_pconnect () useful for?

Description ¶ Establishes a persistent connection to a MySQL server. 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.

Is mysql_connect deprecated?

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in. Since PHP 5.5 has removed support for mysql extension in favor of mysqli. It's highly recommended to upgrade to phpGrid 6.0 to address mysql extension deprecation.

Which MySQL command can be used as replacement for the Mysql_select_db ()?

Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide. Alternatives to this function include: mysqli_select_db()


2 Answers

If you are going to write a web page there is no need of persistent connection. It takes too much resources. Use mysql_connect. Minimize the time your db connection is open and not used as much as you can. Open, fetch what you want, close. It doesn't need to stay open while the users are just reading. The connection will be used eventually if they respond - INSERT/go to another page..

Here are some good points about NOT USING persistent connection in web applications

  • When you lock a table, normally it is unlocked when the connection closes, but since persistent connections do not close, any tables you accidentally leave locked will remain locked, and the only way to unlock them is to wait for the connection to timeout or kill the process. The same locking problem occurs with transactions. (See comments below on 23-Apr-2002 & 12-Jul-2003)

  • Normally temporary tables are dropped when the connection closes, but since persistent connections do not close, temporary tables aren't so temporary. If you do not explicitly drop temporary tables when you are done, that table will already exist for a new client reusing the same connection. The same problem occurs with setting session variables. (See comments below on 19-Nov-2004 & 07-Aug-2006)

  • If PHP and MySQL are on the same server or local network, the connection time may be negligible, in which case there is no advantage to persistent connections.

  • Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the available children which already has a persistent connection open, it tends to spawn a new child, which must then open a new database connection. This causes excess processes which are just sleeping, wasting resources, and causing errors when you reach your maximum connections, plus it defeats any benefit of persistent connections. (See comments below on 03-Feb-2004, and the footnote at http://devzone.zend.com/node/view/id/686#fn1)

like image 198
Svetlozar Angelov Avatar answered Nov 02 '22 00:11

Svetlozar Angelov


You should also look at mysqli and pdo. mysql-extension is pretty old and does not support prepared statements mysqli does. And pdo supports multiple databases without changing queries.

like image 23
MrHus Avatar answered Nov 02 '22 01:11

MrHus