<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
$mysqli = new mysqli("127.0.0.1", "user", "password", "database", 3306);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $mysqli->host_info . "\n";
?>
The above example will output:
Localhost via UNIX socket
127.0.0.1 via TCP/IP
Connection parameter defaults
Depending on the connection function used, assorted parameters can be omitted. If a parameter is not
provided, then the extension attempts to use the default values that are set in the PHP configuration
file.
Example 20.81. Setting defaults
mysqli.default_host=192.168.2.27
mysqli.default_user=root
mysqli.default_pw=""
mysqli.default_port=3306
mysqli.default_socket=/tmp/mysql.sock
The resulting parameter values are then passed to the client library that is used by the extension. If the
client library detects empty or unset parameters, then it may default to the library built-in values.
Built-in connection library defaults
If the host value is unset or empty, then the client library will default to a Unix socket connection on
localhost. If socket is unset or empty, and a Unix socket connection is requested, then a connection
to the default socket on
On Windows systems, the host name
Windows named pipe based connection. In this case the socket parameter is interpreted as the pipe
name. If not given or empty, then the socket (pipe name) defaults to \\.\pipe\MySQL.
If neither a Unix domain socket based not a Windows named pipe based connection is to be be
established and the port parameter value is unset, the library will default to port 3306.
The
mysqlnd
library and the MySQL Client Library (libmysqlclient) implement the same logic for
determining defaults.
Connection options
Connection options are available to, for example, set init commands which are executed upon connect,
or for requesting use of a certain charset. Connection options must be set before a network connection
is established.
MySQL Improved Extension (Mysqli)
is attempted.
/tmp/mysql.sock
is interpreted by the client library as an attempt to open a
.
2313
Need help?
Do you have a question about the 5.0 and is the answer not in the manual?
Questions and answers