PHP mysqli_options() Function
Definition
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().
Syntax
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
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 |
Return
It returns TRUE on success and FALSE on failure.
Example 1
The following code opens a new connection to the MySQL server.
<?php/*from w w w .j av a2 s .c om*/
$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);
?>
Example 2
<?php/* w ww.ja va2 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();
?>