PHP 8.4.0 RC4 available for testing

mysqli::options

mysqli_options

(PHP 5, PHP 7, PHP 8)

mysqli::options -- mysqli_optionsSet options

Опис

Об'єктно-орієнтований стиль

public mysqli::options(int $option, string|int $value): bool

Процедурний стиль

mysqli_options(mysqli $mysql, int $option, string|int $value): bool

Used to set extra connect options and affect behavior for a connection.

This function may be called multiple times to set several options.

mysqli_options() should be called after mysqli_init() and before mysqli_real_connect().

Параметри

mysql

Тільки процедурний стиль: об'єкт mysqli, якого повертає функція mysqli_connect() або mysqli_init()

option

The option that you want to set. It can be one of the following values:

Valid options
Name Description
MYSQLI_OPT_CONNECT_TIMEOUT Connection timeout in seconds
MYSQLI_OPT_READ_TIMEOUT Command execution result timeout in seconds. Available as of PHP 7.2.0.
MYSQLI_OPT_LOCAL_INFILE Enable/disable use of LOAD LOCAL INFILE
MYSQLI_INIT_COMMAND Command to execute after when connecting to MySQL server
MYSQLI_SET_CHARSET_NAME The charset to be set as default.
MYSQLI_READ_DEFAULT_FILE Read options from named option file instead of my.cnf Not supported by mysqlnd.
MYSQLI_READ_DEFAULT_GROUP Read options from the named group from my.cnf or the file specified with MYSQL_READ_DEFAULT_FILE. Not supported by mysqlnd.
MYSQLI_SERVER_PUBLIC_KEY RSA public key file used with the SHA-256 based authentication.
MYSQLI_OPT_NET_CMD_BUFFER_SIZE The size of the internal command/network buffer. Only valid for mysqlnd.
MYSQLI_OPT_NET_READ_BUFFER_SIZE Maximum read chunk size in bytes when reading the body of a MySQL command packet. Only valid for mysqlnd.
MYSQLI_OPT_INT_AND_FLOAT_NATIVE Convert integer and float columns back to PHP numbers when using non-prepared statements. Only valid for mysqlnd.
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT Whether to verify server certificate or not.

value

The value for the option.

Значення, що повертаються

Повертає true у разі успіху або false в разі помилки.

Помилки/виключення

Якщо увімкнені звіти про помилки mysqli (MYSQLI_REPORT_ERROR) і запитана операція не виконалась, буде виведено попередження, але якщо встановлено режим MYSQLI_REPORT_STRICT, буде викинуто виключення mysqli_sql_exception.

Приклади

See mysqli_real_connect().

Примітки

Зауваження:

MySQLnd завжди використовує стандартне кодування символів на сервері. Це кодування надсилається на етапі з'єднання/автентифікації, які використовує mysqlnd.

Libmysqlclient використовує кодування символів, вказане у файлі my.cnf, або явним викликом функції mysqli_options() перед викликом mysqli_real_connect(), але після mysqli_init().

Прогляньте також

add a note

User Contributed Notes 6 notes

up
8
php at darkain dot com
6 years ago
There is an undocumented option: MYSQLI_OPT_READ_TIMEOUT. This is similar to MYSQLI_OPT_CONNECT_TIMEOUT in theory, but has a slightly different application. Connection timeout only specifies the wait time for the initial TCP connection. Once that is created, the timeout no longer applies. Read timeout, however, is from the time the TCP connection is created until the first packet of actual data is received. There are instances where a TCP connection can be established, but the MySQL server stalls indefinitely, preventing execution from ever returning to PHP. Specifying a read timeout alleviates this condition, whereas connect timeout wouldn't.

If the MYSQLI_OPT_READ_TIMEOUT constant isn't defined, it is still supported on versions where that isn't the case. You can define it yourself in older PHP versions with the following code.

<?php
if (!defined('MYSQLI_OPT_READ_TIMEOUT')) {
define ('MYSQLI_OPT_READ_TIMEOUT', 11);
}
?>

You can then use read timeout the same way you could a connect timeout as follows. Please note that since these are two different timeout values for two different parts of the entire connection process, the timeouts do stack (eg: 10 seconds connect timeout + 10 seconds read timeout = maximum possible timeout of 20 seconds)

<?php
//create the object
$connection = mysqli_init();

//specify the connection timeout
$connection->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);

//specify the read timeout
$connection->options(MYSQLI_OPT_READ_TIMEOUT, 10);

//initiate the connection to the server, using both previously specified timeouts
$connection->real_connect('server', 'user', 'pass', 'database');
?>
up
8
fluppy
17 years ago
Here es little example to create a SSL Connection

<?php

$db
= mysqli_init();

/*
When you want so use a separate cnf
$test = $db->options(MYSQLI_EAD_DEFAULT_FILE,'myother.cnf');
*/

$db->ssl_set('server-key.pem','server-cert.pem',
'cacert.pem',NULL,NULL);

$db->real_connect('localhost','root','','mydb');

//Here some query

$db->close();

?>
up
2
Procedural Man
2 years ago
Although it is not explained on the manual, MYSQLI_OPT_SSL_VERIFY_SERVER_CERT is an option only valid for mysqlnd and will raise an error if used with mysqli.
up
1
puneetsharam9 at hotmail dot com
2 years ago
With Objective Approach
init of mysqli is depreciated from 8.1 it seem so
You could have to use an empty __construct()
So You have proper int and float

class DB extends \mysqli {
private function __construct(
private $_user = DBUSER,
private $_pass = DBPWD,
private $_dbName = DBNAME,
private $_dbHost = DBHOST,
) {
parent::__construct();
parent::options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
parent::real_connect($this->_dbHost, $this->_user, $this->_pass, $this->_dbName);
}
}
up
2
Guy Sartorelli
1 year ago
The `MYSQLI_OPT_SSL_VERIFY_SERVER_CERT` seems to always fail, with `options()` always returning false.
Use the `MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT` flag with `real_connect()` instead.
up
-2
king at bobfish dot org
16 years ago
Example on using mysqli_options to increase size of max_allowed_packet for working with big blobs.

function dbConnect()
{
$user = 'jomama';
$pass = 'cartoon';
$dbName = 'LifeCycle';
$host = 'localhost';

$mysqli = mysqli_init();
mysqli_options($mysqli,MYSQLI_READ_DEFAULT_GROUP,
"max_allowed_packet=50M");
mysqli_real_connect($mysqli,$host, $user, $pass,$dbName)
or die ('<P>Unable to connect</P>');

return $mysqli;
}
To Top