PHP mysql_pconnect() Function

Definition and Usage

The mysql_pconnect() function opens a persistent connection to the MySQL server.

mysql_pconnect() and mysql_connect() are very similar, but there are two main differences:

  1. When connecting, this function will first try to find an existing (persistent) connection with the same username and password on the same host. If found, it will return the connection identifier without opening a new connection.
  2. Secondly, after the script execution is completed, the connection to the SQL server will not be closed. This connection will remain open for future use (mysql_close() will not close the connection established by mysql_pconnect()).

Syntax

mysql_pconnect(server,user,pwd,clientflag)
Parameter Description
server

Optional. Specifies the server to connect to.

It can include a port number, such as "hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If the PHP instruction mysql.default_host is not defined (the default case), the default value is 'localhost:3306'.

user Optional. Username. The default value is the username of the server process owner.
pwd Optional. Password. The default value is an empty password.
clientflag

Optional.clientflags The parameter can be a combination of the following constants:

  • MYSQL_CLIENT_SSL - Uses SSL encryption
  • MYSQL_CLIENT_COMPRESS - Uses compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE - Allows spaces after the function name
  • MYSQL_CLIENT_INTERACTIVE - Allows closing the connection before the interactive timeout inactive time

Return Value

If successful, it returns a MySQL persistent connection identifier, and FALSE is returned if an error occurs.

Tips and Comments

Note:Optional parameter clientflag Available since PHP 4.3.0 version.

Tip:To create a non-persistent connection, use mysql_connect() Functions.

Examples

<?php
$con = mysql_pconnect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
?>