PHP mysqli options() Method
The PHP mysqli::options() / mysqli_options() function is used to set extra connect options and affect behavior for a connection. This function may be called multiple times to set several options. This function should be called after mysqli_init() and before mysqli_real_connect().
Syntax
//Object-oriented style public mysqli::options(option, value) //Procedural style mysqli_options(mysql, option, value)
Parameters
mysql |
Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init(). |
option |
Required. Specify the option to set. It can be one of the following values:
|
value |
Required. Specify the value for the option. |
Return Value
Returns true on success or false on failure.
Example: Object-oriented style
The example below shows the usage of mysqli::options() 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_options() 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