W3docs

mysqli_options()

Learn how the PHP mysqli_options() function sets extra connection options, such as timeouts and LOCAL INFILE, before opening a MySQLi connection.

The mysqli_options() function sets extra connection options that tune how PHP talks to MySQL. This guide explains what each common option does, the strict rule about when you may call it, and how to combine it with mysqli_real_connect() in a real connection sequence.

Introduction to the mysqli_options() function

mysqli_options() configures the behavior of a MySQLi connection handle before the connection is opened. It is the procedural equivalent of the object-oriented mysqli::options() method.

The key thing to understand is the order of operations. A normal mysqli_connect() call both creates the handle and connects it in one step, leaving no window to set options. To use mysqli_options() you must split those two steps apart:

  1. Create an unconnected handle with mysqli_init().
  2. Set one or more options with mysqli_options().
  3. Open the actual connection with mysqli_real_connect().

Setting an option after the connection is already established either has no effect or fails, depending on the option.

Syntax

mysqli_options(mysqli $mysql, int $option, mixed $value): bool
  • $mysql — a connection handle returned by mysqli_init() (not yet connected).
  • $option — one of the MYSQLI_* option constants (see below).
  • $value — the value for that option; the expected type depends on the option.

The function returns true on success and false on failure.

Common option constants

ConstantValue typePurpose
MYSQLI_OPT_CONNECT_TIMEOUTinteger (seconds)Maximum time to wait while opening the connection.
MYSQLI_OPT_READ_TIMEOUTinteger (seconds)Maximum time to wait for a query result.
MYSQLI_OPT_LOCAL_INFILE0 or 1Enable or disable LOAD DATA LOCAL INFILE.
MYSQLI_INIT_COMMANDstringA SQL statement run automatically after connecting/reconnecting.
MYSQLI_OPT_INT_AND_FLOAT_NATIVE0 or 1Return integer and float columns as native PHP types (mysqlnd only).

How to use the mysqli_options() function

The example below initializes a handle, sets a connect timeout and enables local file loading, then opens the connection:

<?php
$mysqli = mysqli_init();

/* Set connection timeout to 10 seconds */
mysqli_options($mysqli, MYSQLI_OPT_CONNECT_TIMEOUT, 10);

/* Enable LOAD DATA LOCAL INFILE */
mysqli_options($mysqli, MYSQLI_OPT_LOCAL_INFILE, 1);

/* Now open the actual connection */
if (!mysqli_real_connect($mysqli, "localhost", "username", "password", "database")) {
    die("Connection failed: " . mysqli_connect_error());
}

echo "Connected successfully";
?>

Here we first create an unconnected handle with mysqli_init(), configure the timeout and local-file behavior with mysqli_options(), and only then connect with mysqli_real_connect(). The connection result is checked with mysqli_connect_error(), which returns a description of the last connection failure.

Running a command right after connecting

MYSQLI_INIT_COMMAND is handy when every connection should start in a known state — for example forcing a session character set or time zone. The statement runs after each connect and reconnect:

<?php
$mysqli = mysqli_init();

mysqli_options($mysqli, MYSQLI_INIT_COMMAND, "SET NAMES 'utf8mb4'");

if (!mysqli_real_connect($mysqli, "localhost", "username", "password", "database")) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

Common gotchas

  • Call it before connecting. This is the most frequent mistake. Use mysqli_init() + mysqli_options() + mysqli_real_connect(), never plain mysqli_connect().
  • SSL is separate. Certificate, key, and CA paths are configured with mysqli_ssl_set(), not with mysqli_options().
  • MYSQLI_OPT_LOCAL_INFILE is a security setting. Only enable it if you actually need LOAD DATA LOCAL INFILE; enabling it can let a compromised server read local files.
  • Check the return value. mysqli_options() returns false for unsupported options, so it is worth verifying when an option silently has no effect.

Conclusion

mysqli_options() lets you fine-tune a MySQLi connection — timeouts, local-file loading, and startup commands — but only within the mysqli_init()mysqli_options()mysqli_real_connect() sequence. To continue with MySQLi connections, see mysqli_connect() and how to diagnose failures with mysqli_connect_error().

Practice

Practice
Which sequence correctly applies options before opening a MySQLi connection?
Which sequence correctly applies options before opening a MySQLi connection?
Was this page helpful?