PHP | mysqli_options() Function

PHP mysqli_options() Function


Hello folks! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_options() Function.

The built-in PHP mysqli_options() function is used for setting extra connection option. If you want to set multiple options then you need to invoke the PHP function multiple times.

Syntax

Following below is the syntax to use this function -

mysqli_options($con, $option, $value)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

2

option(Mandatory)

This is an integer value representing the connection option you want to 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(Mandatory)

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


Return Value

This built-in PHP function returns true on success and false on failure.

PHP Version

This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.

Example1

The following below is an example which demonstrates the usage of PHP mysqli_options() function (in a procedural style) -

<?php
   //Creating the 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

When the above code is executed, it will produce the following result -

Connection Established Successfully

Example2

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

<?php
   //Creating the 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

When the above code is executed, it will produce the following result -

Connection Established Successfully

Example3

The following below is another example of the built-in PHP mysqli_options() function in an object oriented style $minus;

<?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("Connect Error: " . mysqli_connect_error());
   }else{
      print("Connection was successful");
   }   
?>

Output

When the above code is executed, it will produce the following result -

Connection was successful


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the PHP mysqli_ping() Function.

Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.