The mysqli_options() function, which can be called several times, sets connect options and change connection settings.
The mysqli_options() function should be called after mysqli_init() and before mysqli_real_connect().
PHP mysqli_options() Function has the following syntax.
Object oriented style
bool mysqli::options ( int $option , mixed $value )
Procedural style
bool mysqli_options ( mysqli $link , int $option , mixed $value )
Parameter | Is Required | Description |
---|---|---|
connection | Required. | MySQL connection to use |
option | Required. | Option to set. |
value | Required. | Value for the option |
option can be one of the following values.
Value | Meaning |
---|---|
MYSQLI_OPT_CONNECT_TIMEOUT | connection timout in seconds |
MYSQLI_OPT_LOCAL_INFILE | enable/disable use of LOAD LOCAL INFILE |
MYSQLI_INIT_COMMAND | command to execute after connecting to MySQL server |
MYSQLI_READ_DEFAULT_FILE | read options from named file instead of my.cnf |
MYSQLI_READ_DEFAULT_GROUP | read options from named group from my.cnf or the file specified in MYSQLI_READ_DEFAULT_FILE |
MYSQLI_SERVER_PUBLIC_KEY | RSA public key file used with SHA-256 based authentication |
It returns TRUE on success and FALSE on failure.
The following code opens a new connection to the MySQL server.
<?php/* w ww . j ava 2 s . c o m*/
$con=mysqli_init();
if (!$con){
die("mysqli_init failed");
}
mysqli_options($con,MYSQLI_READ_DEFAULT_FILE,"myfile.cnf");
if (!mysqli_real_connect($con,"localhost","my_user","my_password","my_db")){
die("Connect Error: " . mysqli_connect_error());
}
mysqli_close($con);
?>
<?php// w w w . j a v a 2 s. com
$mysqli = mysqli_init();
if (!$mysqli) {
die('mysqli_init failed');
}
if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
die('Setting MYSQLI_INIT_COMMAND failed');
}
if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
}
if (!$mysqli->real_connect('localhost', 'my_user', 'my_password', 'my_db')) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
}
echo $mysqli->host_info;
$mysqli->close();
?>