Tutorials References Exercises Videos Menu
Free Website Get Certified Pro

PHP Tutorial

PHP HOME PHP Intro PHP Install PHP Syntax PHP Comments PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Numbers PHP Math PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP Loops PHP Functions PHP Arrays PHP Superglobals PHP RegEx

PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

PHP Advanced

PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP Exceptions

PHP OOP

PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Interfaces PHP Traits PHP Static Methods PHP Static Properties PHP Namespaces PHP Iterables

MySQL Database

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP Examples

PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Certificate

PHP Reference

PHP Overview PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP mysqli options() Function

❮ PHP MySQLi Reference

Example - Object Oriented style

Set extra connect options:

<?php
$mysqli = mysqli_init();
if (!$mysqli) {
  die("mysqli_init failed");
}

// Specify connection timeout
$con -> options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);

// Specify read options from named file instead of my.cnf
$con -> options(MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");

$con -> real_connect("localhost","my_user","my_password","my_db");
?> 

Look at example of procedural style at the bottom.


Definition and Usage

The options() / mysqli_options() function is used to set extra connect options and affect behavior for a connection.

Note: This function should be called after init() and before real_connect().


Syntax

Object oriented style:

$mysqli -> options(option, value)

Procedural style:

mysqli_options(connection, option, value)

Parameter Values

Parameter Description
connection Required. Specifies the MySQL connection to use
option Required. Specifies the option to set. Can be one of the following values:
  • MYSQLI_OPT_CONNECT_TIMEOUT - Set connection timeout in seconds
  • MYSQLI_OPT_LOCAL_INFILE - Enable/Disable use of LOAD LOCAL INFILE
  • MYSQLI_INIT_COMMAND - Set a command to execute after connecting to MySQL server
  • MYSQLI_READ_DEFAULT_FILE - Set read options from named file instead of my.cnf
  • MYSQLI_READ_DEFAULT_GROUP - Set read options from named group from my.cnf or the file specified in MYSQLI_READ_DEFAULT_FILE
  • MYSQLI_SERVER_PUBLIC_KEY - Set RSA public key file used with SHA-256 based authentication
  • MYSQLI_OPT_NET_CMD_BUFFER_SIZE - only for mysqlnd
  • MYSQLI_OPT_NET_READ_BUFFER_SIZE - only for mysqlnd
  • MYSQLI_OPT_INT_AND_FLOAT_NATIVE - only for mysqlnd
  • MYSQLI_OPT_SSL_VERIFY_SERVER_CERT - only for mysqlnd
value Required. Specifies the value for the option

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 5+
PHP Changelog: PHP 5.5: Added MYSQLI_SERVER_PUBLIC_KEY option
PHP 5.3: Added MYSQLI_OPT_INT_AND_FLOAT_NATIVE, MYSQLI_OPT_NET_CMD_BUFFER_SIZE, MYSQLI_OPT_NET_READ_BUFFER_SIZE, and MYSQLI_OPT_SSL_VERIFY_SERVER_CERT options

Example - Procedural style

Set extra connect options:

<?php
$con = mysqli_init();
if (!$con) {
  die("mysqli_init failed");
}

// Specify connection timeout
mysqli_options($con, MYSQLI_OPT_CONNECT_TIMEOUT, 10);

// Specify read options from named file instead of my.cnf
mysqli_options($con, MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");

mysqli_real_connect($con,"localhost","my_user","my_password","my_db");
?> 


❮ PHP MySQLi Reference