PHP mysqli_init() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_init() creates a new mysqli object that is not connected yet, so you can set options before opening the server connection.

The usual flow is mysqli_init() → mysqli_options() (optional) → mysqli_real_connect(). For a simple login, mysqli_connect() or new mysqli() is enough and skips this setup path.

Syntax

Call mysqli_init() with the values you need. Start with this line.

PHP
$mysqli = mysqli_init();
CallWhat it does
mysqli_init()Creates an unconnected mysqli handle (or false on failure).
$m = mysqli_init(); mysqli_options($m, MYSQLI_OPT_CONNECT_TIMEOUT, 5);Sets a connect timeout before real_connect().
mysqli_real_connect(mysqli_init(), "localhost", "u", "p", "db")Init then connect in one expression (options must be set in between if needed).

mysqli_init() alone does not talk to MySQL. You must call mysqli_real_connect() (or related connect APIs) before query(). Object-oriented code often uses new mysqli() without arguments, then options() and real_connect().

Library declarations
PHP
mysqli_init(): mysqli|false

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
mysqli|falseA mysqli object ready for options and real_connect, or false on failure.

PHP Examples

Example 1 — Init Then Real Connect

Create a handle, connect, then print host info. Sample output is fixed.

PHP
<?php
$mysqli = mysqli_init();

if (!$mysqli) {
    echo "Init failed.", PHP_EOL;
    exit;
}

$ok = mysqli_real_connect(
    $mysqli,
    "localhost",
    "demo_user",
    "demo_pass",
    "app_db"
);

echo $ok ? "Connected." : "Connect failed.", PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. mysqli_init() builds an empty mysqli object.
  2. mysqli_real_connect() opens the TCP/socket session.
  3. On success the handle works like a normal connection.
  4. Close when finished.

Example 2 — Set Options Before Connecting

Show why init exists: options must run before real_connect.

PHP
<?php
$mysqli = mysqli_init();
mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
mysqli_options($mysqli, MYSQLI_INIT_COMMAND, "SET NAMES utf8mb4");

mysqli_real_connect($mysqli, "localhost", "demo_user", "demo_pass", "app_db");

echo "Timeout option applied before connect.", PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Create the handle with mysqli_init().
  2. mysqli_options() configures timeout and an init SQL command.
  3. Only then call mysqli_real_connect().
  4. Calling options after connect is too late for connect-time settings.

Example 3 — Simple Connect Without Init

Contrast the everyday one-step connect path.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

if ($mysqli) {
    echo "One-step connect works for most apps.", PHP_EOL;
    mysqli_close($mysqli);
}

How It Works

  1. mysqli_connect() creates and connects in one call.
  2. new mysqli(. ..) does the same in object style.
  3. Use init/real_connect when you need pre-connect options.
  4. Both paths end with a normal mysqli connection object.

Frequently Asked Questions

It creates a mysqli object that is not connected yet, so you can set options before mysqli_real_connect().
No. Most scripts use mysqli_connect() or new mysqli(). Init is for advanced setup.
Optional mysqli_options() / mysqli_ssl_set(), then mysqli_real_connect().
No. connect opens the server immediately. init only prepares the object.
No. You must real_connect first, or you will get connection errors.

Next: mysqli_insert_id()

mysqli_insert_id() returns the AUTO_INCREMENT value generated by the last INSERT (or UPDAT.

mysqli_insert_id() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful