MySQL Improved Extension (
Mysqli
)
2314
For setting a connection option, the connect operation has to be performed in three steps: creating a
connection handle with
mysqli_init
, setting the requested options using
mysqli_options
, and
establishing the network connection with
mysqli_real_connect
.
Connection pooling
The mysqli extension supports persistent database connections, which are a special kind of pooled
connections. By default, every database connection opened by a script is either explicitly closed by the
user during runtime or released automatically at the end of the script. A persistent connection is not.
Instead it is put into a pool for later reuse, if a connection to the same server using the same username,
password, socket, port and default database is opened. Reuse saves connection overhead.
Every PHP process is using its own mysqli connection pool. Depending on the web server deployment
model, a PHP process may serve one or multiple requests. Therefore, a pooled connection may be
used by one or more scripts subsequently.
Persistent connection
If a unused persistent connection for a given combination of host, username, password, socket,
port and default database can not be found in the connection pool, then mysqli opens a new
connection. The use of persistent connections can be enabled and disabled using the PHP directive
mysqli.allow_persistent
. The total number of connections opened by a script can be limited with
mysqli.max_links
. The maximum number of persistent connections per PHP process can be restricted
with
mysqli.max_persistent
. Please note, that the web server may spawn many PHP processes.
A common complain about persistent connections is that their state is not reset before reuse. For
example, open and unfinished transactions are not automatically rolled back. But also, authorization
changes which happened in the time between putting the connection into the pool and reusing it are
not reflected. This may be seen as an unwanted side-effect. On the contrary, the name
persistent
may be understood as a promise that the state is persisted.
The mysqli extension supports both interpretations of a persistent connection: state persisted, and
state reset before reuse. The default is reset. Before a persistent connection is reused, the mysqli
extension implicitly calls
mysqli_change_user
to reset the state. The persistent connection appears
to the user as if it was just opened. No artifacts from previous usages are visible.
The
mysqli_change_user
function is an expensive operation. For best performance, users may
want to recompile the extension with the compile flag
MYSQLI_NO_CHANGE_USER_ON_PCONNECT
being set.
It is left to the user to choose between safe behavior and best performance. Both are valid optimization
goals. For ease of use, the safe behavior has been made the default at the expense of maximum
performance.
See also
mysqli::__construct
mysqli::init
mysqli::options
mysqli::real_connect
mysqli::change_user
$mysqli::host_info
MySQLi Configuration Options
Persistent Database Connections
20.7.3.3.3. Executing statements
Copyright 1997-2012 the PHP Documentation Group. [2230]
Statements can be executed with the
mysqli_query
,
mysqli_real_query
and
mysqli_multi_query
functions. The
mysqli_query
function is the most common, and combines
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...