Name
mysql_pconnect
Synopsis
cresource mysql_pconnect([stringhostname
[, stringusername
[, stringpassword
]]])
Used to
establish or re-establish a connection to the MySQL DBMS. The
function returns a connection resource handle on success that can be
used to access databases through subsequent function calls. The
function returns false
on failure. The three
parameters are identical to the first three parameters of
mysql_connect( )
.
This function should be called only once with the same parameters in
a script: any subsequent calls to mysql_pconnect(
)
in the same script with the same parameters return the
same connection handle. Indeed, connections created with
mysql_pconnect( )
are often reused across several
scripts: the p
stands for persistent, which means
that after the script ends, the connection is kept in a pool. The
connection can then be reused by any other script that requires a
connection with the same hostname
,
username
, and
password
.
Connections in the pool that remain unused are closed to save resources. How long a connection can remain unused is a MySQL parameter and is set to a default of five seconds. This can be changed with the --set-variable connect_timeout parameter to safe_mysqld.
Persistent connections are available only through a PHP module that is integrated into a web server. See Chapter 11 for details.
Example
<?php $query = "SELECT * FROM presents"; $connection = mysql_pconnect("localhost", "fred", "shhh"); mysql_select_db("wedding", $connection); $result ...
Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.