PHP mysqli_options() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_options() sets a connection option on a mysqli handle created with mysqli_init(), before you call mysqli_real_connect().

Common options include MYSQLI_OPT_CONNECT_TIMEOUT and MYSQLI_INIT_COMMAND. mysqli_set_opt() is an alias of mysqli_options() — same function, other name (documented in the set-opt catalog page).

Syntax

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

PHP
mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
CallWhat it does
mysqli_options($m, MYSQLI_OPT_CONNECT_TIMEOUT, 5)Sets a 5-second connect timeout.
mysqli_options($m, MYSQLI_INIT_COMMAND, "SET AUTOCOMMIT=0")Runs a command automatically right after connect.
mysqli_set_opt($m, MYSQLI_OPT_CONNECT_TIMEOUT, 5)Alias of mysqli_options() with the same arguments.

Call options after init and before real_connect. Setting connect-time options on an already connected handle is too late. For charset after connect, use mysqli_set_charset() instead of guessing with init commands alone.

Library declarations
PHP
mysqli_options(mysqli $mysql, int $option, string|int $value): bool

Parameters

ParameterDescription
mysqlRequired. A mysqli object from mysqli_init() (not yet fully connected).
optionRequired. An option constant such as MYSQLI_OPT_CONNECT_TIMEOUT.
valueRequired. The option value as string or int, depending on the option.

Return Value

Return ValueDescription
booltrue if the option was accepted, otherwise false.

PHP Examples

Example 1 — Set a Connect Timeout

Init, set timeout, then real_connect. Sample reports success.

PHP
<?php
$mysqli = mysqli_init();
mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 5);

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

echo $ok ? "Connected with timeout option." : "Failed.", PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. mysqli_init() creates the empty handle.
  2. mysqli_options() sets MYSQLI_OPT_CONNECT_TIMEOUT to 5 seconds.
  3. mysqli_real_connect() opens the server using that setting.
  4. Close when finished.

Example 2 — Multiple Options Before Connect

Call mysqli_options() more than once for several settings.

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

mysqli_real_connect($mysqli, "localhost", "demo_user", "demo_pass", "app_db");
echo "Options applied.", PHP_EOL;
mysqli_close($mysqli);

How It Works

  1. Each mysqli_options() call sets one flag.
  2. INIT_COMMAND runs automatically after a successful connect.
  3. You can still call mysqli_set_charset() afterward for clarity.
  4. Order matters: all options first, then real_connect.

Example 3 — Alias mysqli_set_opt()

Demonstrate that set_opt is the same API under another name.

PHP
<?php
$mysqli = mysqli_init();

// Equivalent:
mysqli_set_opt($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
// mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 5);

echo "set_opt is an alias of options.", PHP_EOL;

How It Works

  1. mysqli_set_opt() calls the same underlying feature as mysqli_options().
  2. Pick one name in your project and stay consistent.
  3. The dedicated set-opt page covers the alias in the same catalog set.
  4. No behavioral difference is expected between the two names.

Frequently Asked Questions

It configures a mysqli handle from mysqli_init() before mysqli_real_connect().
No. mysqli_set_opt() is an alias of mysqli_options().
After init and before real_connect for connect-time options.
You can use INIT_COMMAND, but mysqli_set_charset() after connect is the clear, recommended approach.
Usually not. mysqli_connect() is enough until you need timeouts or special connect behavior.

Next: mysqli_ping()

mysqli_ping() checks whether the MySQL connection is still alive and returns true if the s.

mysqli_ping() 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