PHP Function Reference

PHP mysqli init() Method



The PHP mysqli::init() / mysqli_init() function allocates or initializes a MYSQL object for use with mysqli_options() and mysqli_real_connect().

Note: Any subsequent calls to any mysqli function (except mysqli_options() and mysqli_ssl_set()) will fail until mysqli_real_connect() was called.

Syntax

//Object-oriented style
public mysqli::init()

//Procedural style
mysqli_init()

Parameters

No parameter is required.

Return Value

Returns an object.

Example: Object-oriented style

The example below shows the usage of mysqli::init() 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_init() 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