PHP mysqli set_opt() Method
The PHP mysqli::set_opt() / mysqli_set_opt() 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().
This method is an alias of mysqli_options() function.
Syntax
//Object-oriented style public mysqli::set_opt(option, value) //Procedural style mysqli_set_opt(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::set_opt() method.
<?php $mysqli = mysqli_init(); if (!$mysqli) { die('mysqli_init failed'); } if (!$mysqli->set_opt(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) { die('Setting MYSQLI_INIT_COMMAND failed'); } //specifying connection timeout if (!$mysqli->set_opt(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::set_opt(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) { die('Setting MYSQLI_INIT_COMMAND failed'); } if (!parent::set_opt(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_set_opt() function.
<?php $mysqli = mysqli_init(); if (!$mysqli) { die('mysqli_init failed'); } if (!mysqli_set_opt($mysqli, MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) { die('Setting MYSQLI_INIT_COMMAND failed'); } //specifying connection timeout if (!mysqli_set_opt($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