mysqli::options

mysqli_options

(PHP 5, PHP 7, PHP 8)

mysqli::options -- mysqli_options设置选项

说明

面向对象风格

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

过程化风格

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

用于设置连接的扩展选项,这些选项可以改变这个连接的行为。

如果要对多个选项进行设置,可以多次调用此函数来。

mysqli_options() 需要在 mysqli_init() 函数之后、 mysqli_real_connect() 函数之前调用。

参数

mysql

仅以过程化样式:由 mysqli_connect()mysqli_init() 返回的 mysqli 对象。

option

要进行设置的选项,可以是下列值中的某一项:

有效的选项
名称 描述
MYSQLI_OPT_CONNECT_TIMEOUT 连接超时设置,以秒为单位
MYSQLI_OPT_READ_TIMEOUT 命令执行超时秒数,自 PHP 7.2.0 起可用。
MYSQLI_OPT_LOCAL_INFILE 启用或禁用 LOAD LOCAL INFILE
MYSQLI_INIT_COMMAND 连接到 MySQL 服务器后要执行的命令
MYSQLI_SET_CHARSET_NAME 设置的默认字符集
MYSQLI_READ_DEFAULT_FILE 从指定的文件中读取选项,而不是使用 my.cnf 中的选项。mysqlnd 不支持。
MYSQLI_READ_DEFAULT_GROUP my.cnf 或者 MYSQL_READ_DEFAULT_FILE 指定的文件中 读取指定的组中的选项。mysqlnd 不支持。
MYSQLI_SERVER_PUBLIC_KEY SHA-256 认证模式下,要使用的 RSA 公钥文件。
MYSQLI_OPT_NET_CMD_BUFFER_SIZE 内部命令/网络缓冲大小, 仅在 mysqlnd 驱动下有效。
MYSQLI_OPT_NET_READ_BUFFER_SIZE 以字节为单位,读取 MySQL 命令报文时候的块大小, 仅在 mysqlnd 驱动下有效。
MYSQLI_OPT_INT_AND_FLOAT_NATIVE 使用非预处理语句时,将整数和浮点数类型的列转换成 PHP 的数值类型。 仅在 mysqlnd 驱动下有效。
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT 是否验证服务器证书。

value

选项值。

返回值

成功时返回 true, 或者在失败时返回 false

错误/异常

If mysqli error reporting is enabled (MYSQLI_REPORT_ERROR) and the requested operation fails, a warning is generated. If, in addition, the mode is set to MYSQLI_REPORT_STRICT, a mysqli_sql_exception is thrown instead.

示例

参见 mysqli_real_connect()

注释

注意:

MySQLnd 总是使用服务器的默认字符集。此字符集在连接握手/认证时发送,并被 mysqlnd 使用。

Libmysqlclient 使用 my.cnf 中的默认字符集或者由在调用 mysqli_init() 之后,mysqli_real_connect() 之前先调用 mysqli_options() 来指定。

参见

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