PHP mysqli_set_opt() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_set_opt() is an alias of mysqli_options() — it sets a connection option before you call real_connect().

Use it on a handle from mysqli_init(). Common options include connect timeouts and init commands. After PHP lookup, prefer documenting options() as the primary name and set_opt() as the alias.

Syntax

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

PHP
$ok = mysqli_set_opt($mysql, MYSQLI_OPT_CONNECT_TIMEOUT, 5);
CallWhat it does
mysqli_set_opt($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)Same effect as mysqli_options() with a connect timeout of 5 seconds.
mysqli_options($link, MYSQLI_OPT_CONNECT_TIMEOUT, 5)The primary function name; set_opt is only an alias.
$mysqli->set_opt(MYSQLI_INIT_COMMAND, 'SET autocommit=0')Object-oriented alias form before real_connect().

Call set_opt/options after init and before real_connect. Calling it on an already connected simple mysqli_connect() handle is the wrong lifecycle for most option flags.

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

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli handle from mysqli_init().
optionRequired. A MYSQLI_OPT_* / related option constant.
valueRequired. The option value as string or int, depending on the option.

Return Value

Return ValueDescription
trueThe option was set.
falseSetting the option failed.

PHP Examples

Example 1 — Alias of options()

Show that both names describe the same call.

PHP
<?php
echo 'mysqli_set_opt === alias of mysqli_options', PHP_EOL;
echo 'example: MYSQLI_OPT_CONNECT_TIMEOUT = 5', PHP_EOL;

How It Works

  1. Read the manual entry for options() for the real behavior.
  2. set_opt() is kept for older naming.
  3. Pass the same constants and values to either name.
  4. Sample output when connected to a demo database is simply a true return before connect.

Example 2 — Correct Lifecycle

init → set_opt → ssl_set (optional) → real_connect.

PHP
<?php
foreach (['mysqli_init', 'mysqli_set_opt', 'mysqli_real_connect'] as $step) {
    echo $step, PHP_EOL;
}

How It Works

  1. Create a handle with mysqli_init().
  2. Apply options with set_opt/options.
  3. Then open the socket with real_connect().
  4. Skipping init/real_connect misses the point of these options.

Example 3 — Teaching Preference

Recommend the clearer primary name in new tutorials.

PHP
<?php
echo 'prefer mysqli_options() in new code', PHP_EOL;
echo 'mention mysqli_set_opt() as alias', PHP_EOL;

How It Works

  1. Aliases confuse beginners if both appear without explanation.
  2. Document one primary API in your project style guide.
  3. Keep set_opt pages for completeness of the MySQLi index.
  4. Behavior and parameters match options().

Frequently Asked Questions

No. set_opt is an alias of options — same parameters and behavior.
After mysqli_init() and before mysqli_real_connect().
Connection-related options such as timeouts and init commands via MYSQLI_* option constants.
Prefer mysqli_options() for clarity, and mention set_opt only as the alias.
Use mysqli_set_charset() for the client charset. Option flags are a different mechanism.

Next: mysqli_sqlstate()

mysqli_sqlstate() returns the SQLSTATE error code from the last MySQLi operation on the co.

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