PHP Function Reference

PHP mysqli real_connect() Method



The PHP mysqli::real_connect() / mysqli_real_connect() function is used to establish a connection to a MySQL database engine. This function differs from mysqli_connect():

  • mysqli_real_connect() requires a valid object which has to be created by function mysqli_init().
  • mysqli_real_connect() can be used with the mysqli_options() function to set various options for connection.
  • mysqli_real_connect() has a flags parameter.

Syntax

//Object-oriented style
public mysqli::real_connect(host, username, passwd, 
                        dbname, port, socket, flags)

//Procedural style
mysqli_real_connect(mysql, host, username, passwd, 
                      dbname, port, socket, flags)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().
host Optional. Specify the host name or an IP address. Passing the null value or "localhost" to this parameter, the local host is assumed.
username Optional. Specify the MySQL user name.
passwd Optional. Specify the MySQL password. This allows the username to be used with different permissions (depending on if a password is provided or not).
dbname Optional. If provided will specify the default database to be used when performing queries.
port Optional. Specify the port number to attempt to connect to the MySQL server.
socket Optional. Specify the socket or named pipe that should be used.
flags Optional. Specify flags to set different connection options. Possible values are:
  • MYSQLI_CLIENT_COMPRESS - Use compression protocol
  • MYSQLI_CLIENT_FOUND_ROWS - return number of matched rows (not the affected rows)
  • MYSQLI_CLIENT_IGNORE_SPACE - Allow spaces after function names. Makes all function names reserved words.
  • MYSQLI_CLIENT_INTERACTIVE - Allow interactive_timeout seconds (instead of wait_timeout seconds) of inactivity before closing the connection
  • MYSQLI_CLIENT_SSL - Use SSL (encryption)
  • MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT - Like MYSQLI_CLIENT_SSL, but disables validation of the provided SSL certificate. This is only for installations using MySQL Native Driver and MySQL 5.6 or later.

Note: For security reasons the MULTI_STATEMENT flag is not supported in PHP. To execute multiple queries use the mysqli_multi_query() function.

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli::real_connect() method.

<?php
$mysqli = mysqli_init();
if (!$mysqli) {
  die('mysqli_init failed');
}

if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
  die('Setting MYSQLI_INIT_COMMAND failed');
}

//specifying connection timeout
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10)) {
  die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}

//establishing connection to the database
if (!$mysqli->real_connect("localhost", "user", "password", "database")) {
  die('Connect Error: '. mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

//closing the connection
$mysqli->close();
?>

The output of the above code will be:

Success... MySQL host info: localhost via TCP/IP

Example: Object-oriented style when extending mysqli class

Consider the example below where this method is used while extending the mysqli class.

<?php
class acs_mysqli extends mysqli {
  public function __construct($host, $user, $pass, $db) {
    parent::init();

    if (!parent::options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
      die('Setting MYSQLI_INIT_COMMAND failed');
    }

    if (!parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 10)) {
      die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
    }

    if (!parent::real_connect($host, $user, $pass, $db)) {
      die('Connect Error: ' . mysqli_connect_error());
    }
  }
}

//establishing connection to the database
$db = new acs_mysqli("localhost", "user", "password", "database");

echo 'Success... ' . $db->host_info . "\n";

//closing the connection
$db->close();
?>

The output of the above code will be:

Success... MySQL host info: localhost via TCP/IP

Example: Procedural style

The example below shows the usage of mysqli_real_connect() function.

<?php
$mysqli = mysqli_init();
if (!$mysqli) {
  die('mysqli_init failed');
}

if (!mysqli_options($mysqli, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
  die('Setting MYSQLI_INIT_COMMAND failed');
}

//specifying connection timeout
if (!mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 10)) {
  die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}

//establishing connection to the database
if (!mysqli_real_connect($mysqli, "localhost", "user", "password", "database")) {
  die('Connect Error: '. mysqli_connect_error());
}

echo 'Success... ' . mysqli_get_host_info($mysqli) . "\n";

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be:

Success... MySQL host info: localhost via TCP/IP

❮ PHP MySQLi Reference