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

ParameterIs RequiredDescription
connectionRequired.MySQL connection to use
optionRequired.Option to set.
valueRequired.Value for the option

option can be one of the following values.

ValueMeaning
MYSQLI_OPT_CONNECT_TIMEOUTconnection timout in seconds
MYSQLI_OPT_LOCAL_INFILEenable/disable use of LOAD LOCAL INFILE
MYSQLI_INIT_COMMANDcommand to execute after connecting to MySQL server
MYSQLI_READ_DEFAULT_FILEread options from named file instead of my.cnf
MYSQLI_READ_DEFAULT_GROUPread options from named group from my.cnf or the file specified in MYSQLI_READ_DEFAULT_FILE
MYSQLI_SERVER_PUBLIC_KEYRSA 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();
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions