English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_options() Function Usage and Example

PHP MySQLi Reference Manual

mysqli_options() function sets options

Definition and Usage

mysqli_options()This feature is used to set extended options for a connection, which can change the behavior of this connection.
If you need to set multiple options, you can call this function multiple times.
The mysqli_options() function must be called after mysqli_init() and before mysqli_real_connect().

Syntax

mysqli_options($con, $option, $value)

Parameter

Serial NumberParameters and Description
1

con(Necessary)

This is an object representing the connection to the MySQL Server.

2

option(Necessary)

Represents the connection options to be set. It can be one of the following:

  • MYSQLI_OPT_CONNECT_TIMEOUT

  • MYSQLI_OPT_LOCAL_INFILE

  • MYSQLI_INIT_COMMAND

  • MYSQLI_READ_DEFAULT_FILE

  • MYSQLI_READ_DEFAULT_GROUP

  • MYSQLI_SERVER_PUBLIC_KEY

  • MYSQLI_OPT_NET_CMD_BUFFER_SIZE

  • MYSQLI_OPT_NET_READ_BUFFER_SIZE

  • MYSQLI_OPT_INT_AND_FLOAT_NATIVE

  • MYSQLI_OPT_SSL_VERIFY_SERVER_CERT

3

value(Necessary)

This is an integer value representing the value of the selected option.

Return value

This function returns a boolean value, and returnstrue, and returnsfalse.

PHP version

This function was originally introduced in PHP version5introduced and can be used in all higher versions.

Online example

The following examples demonstratemysqli_options()Function usage (procedural style)-

<?php
   //Establish connection
   $con = mysqli_connect("localhost", "root", "password", "test");
   mysqli_options($con, MYSQLI_OPT_NET_CMD_BUFFER_SIZE, 15);
   if($con){
      print("Connection established successfully");
   }else{
      print("Connection failed ");
   }
?>

Output result

Connection established successfully

Online example

In the object-oriented style, the syntax of this function is$con-> options();.The following is an example of this function in object-oriented style;

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "test");
   $con->options(MYSQLI_OPT_NET_CMD_BUFFER_SIZE, 15);
   if($con){
      print("Connection established successfully");
   }else{
      print("Connection failed ");
   }
?>

Output result

Connection established successfully

Online example

In the object-oriented style, the syntax of this function is$con-> options();.The following is an example of this function in object-oriented style;

<?php
   $connection_mysql = mysqli_init();
   if (!$connection_mysql){
      die("mysqli_init failed");
   }
   
   mysqli_options($connection_mysql, MYSQLI_OPT_CONNECT_TIMEOUT, 10);
   mysqli_options($connection_mysql, MYSQLI_READ_DEFAULT_FILE, "configure.cnf");
   
   $connection_mysql = mysqli_real_connect($connection_mysql, "localhost", "root", "password", "mydb");
   
   if (!$connection_mysql){
      print("Connection error:  " . mysqli_connect_error());
   }else{
      print("Connection successful");
   }   
?>

Output result

Connection successful

PHP MySQLi Reference Manual