Connection Functions

__construct(
[$s1=ini_get(“mysqli.default_host”)
[,$s2=ini_get(“mysqli.default_user”)
[,$s3=ini_get(“mysqli.default_pw”)
[,$s4=””
[,$i=ini_get(“mysqli.default_port”)
[,$s5=ini_get(“mysqli.default_socket”)]]]]]])
connects to the MySQL server, using the database $s4. Nothing is returned. The procedural form mysqli_connect(……) returns the link. $connect_errno gives the error number for the last connection attempt. Zero means no error. $connect_error gives the error message in the form of a string for the last connection attempt. NULL means no error.  get_connection_stats() gives an array of connection statistics. stat() obtains the status.

init() returns a resource to be used with real_connect(). real_connect([$s1 [,$s2 [,$s3 [, $s4 [, $i [, $s5 [, $i2]]]]]]]) is similar to __construct(……) except that real_connect(……) needs an object created by init(), there is a flags parameter $i2, and various options can be set with options(). The flags $i2 can be
MYSQLI_CLIENT_COMPRESS uses a protocol for compression.
MYSQLI_CLIENT_FOUND_ROWS returns the no. of matched rows, instead of the no. of affected rows.
MYSQLI_CLIENT_IGNORE_SPACE allows spaces beyond function names, and reserves all function names as keywords
MYSQLI_CLIENT_INTERACTIVE allows interactive_timeout (rather than wait_timeout) seconds of inactivity before timeout.
MYSQLI_CLIENT_SSL uses SSL encryption.


options($i, $m) is used to set extra connection options. Where $i can be:

MYSQLI_OPT_CONNECT_TIMEOUT sets the no. of seconds for connection timeout.
MYSQLI_OPT_LOCAL_INFILE enables/disables the use of LOAD LOCAL INFILE.
MYSQLI_INIT_COMMAND is executed during the establishment of a connection to the server.
MYSQLI_READ_DEFAULT_FILE reads settings from the specified file rather than my.cnf.
MYSQLI_READ_DEFAULT_GROUP reads settings from the specified group from my.cnf or the file given by MYSQL_READ_DEFAULT_FILE.
MYSQLI_SERVER_PUBLIC_KEY sets the RSA public key file used with the SHA-256 based authentication.


ssl_set($s1, $s2, $s3, $s4, $s5) establishes secure connections using SSL. $s1 specifies the path to the key. $s2 specifies the path to the certificate. $s3 specifies the path to the certificate authority. $s4 specifies the path to a directory containing trusted SSL CA certificates in PEM format. $s5 specifies a list of allowable ciphers to use for SSL encryption.
ping() pings a server. It attempts to reconnect if the server connection has been lost.

change_user($s1, $s2, $s3)
changes the user using username $s1, password $s2, and database $s3. select_db($s) selects $s as the current database. close() closes the connection.

<?php
$mysqli = mysqli_init();
if (!$mysqli) die('mysqli_init failed');
$mysqli->options(MYSQLI_INIT_COMMAND, 
                          'SET AUTOCOMMIT = 0');
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
if (!$mysqli->real_connect('localhost', 'root', 'pw', 'testDB')) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}
echo $mysqli->stat()."\n";
print_r($mysqli->get_connection_stats());
$mysqli->close();
?>

Uptime: 435665  Threads: 3  Questions: 2624  Slow queries: 0  Opens: 527  Flush tables: 1  Open tables: 104  Queries per second avg: 0.006
Array
(
    [bytes_sent] => 142
    [bytes_received] => 104
    [packets_sent] => 4
    [packets_received] => 3
    [protocol_overhead_in] => 12
    [protocol_overhead_out] => 16
    [bytes_received_ok_packet] => 0
    [bytes_received_eof_packet] => 0
    [bytes_received_rset_header_packet] => 0
    [bytes_received_rset_field_meta_packet] => 0
    [bytes_received_rset_row_packet] => 11
    [bytes_received_prepare_response_packet] => 0
    [bytes_received_change_user_packet] => 0
    [packets_sent_command] => 1
    [packets_received_ok] => 0
    [packets_received_eof] => 0
[packets_received_rset_header] => 0
……