PHP 8.3.4 Released!

mysqli::options

mysqli_options

(PHP 5, PHP 7, PHP 8)

mysqli::options -- mysqli_optionsEstablecer opciones

Descripción

Estilo orientado a objetos

mysqli::options(int $option, mixed $value): bool

Estilo por procedimientos

mysqli_options(mysqli $link, int $option, mixed $value): bool

Se usa para establecer opciones extra y para modificar al comportamiento de una conexión.

Se puede llamar varias veces a esta función para establecer varias opciones.

mysqli_options() debería invocarse después de mysqli_init() y antes de mysqli_real_connect().

Parámetros

link

Sólo estilo por procediminetos: Un identificador de enlace devuelto por mysqli_connect() o mysqli_init()

opción

Opción que se desea establecer. Puede ser una de las siguientes.

Opciones válidas
Nombre Descripción
MYSQLI_OPT_CONNECT_TIMEOUT tiempo de expiración en segundos de la conexión (soportado en Windows con TCP/IP desde PHP 5.3.1)
MYSQLI_OPT_LOCAL_INFILE habilitar/deshabilitar el uso de LOAD LOCAL INFILE
MYSQLI_INIT_COMMAND comando a ejecutar tras conectar al servidor MySQL
MYSQLI_READ_DEFAULT_FILE Leer las opciones del fichero nombrado de opciones en lugar de my.cnf
MYSQLI_READ_DEFAULT_GROUP Leer opciones del grupo nombrado de my.cnf o del fichero especificado con MYSQL_READ_DEFAULT_FILE.
MYSQLI_SERVER_PUBLIC_KEY Fichero de clave pública RSA usado con la autenticación basada en SHA-256.
MYSQLI_OPT_NET_CMD_BUFFER_SIZE El tamaño del búfer interno de órdenes/red. Solo válido para mysqlnd.
MYSQLI_OPT_NET_READ_BUFFER_SIZE Tamaño de trozo de lectura máximo en bytes al leer el cuerpo de un paquete de órdenes de MySQL. Solo válido para mysqlnd.
MYSQLI_OPT_INT_AND_FLOAT_NATIVE Convertir columnas integer y float a números de PHP. Únicamente válido para mysqlnd.
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT

value

Valor de la opción.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Historial de cambios

Versión Descripción
5.5.0 Se añadieron las opciones MYSQLI_SERVER_PUBLIC_KEY y MYSQLI_SERVER_PUBLIC_KEY.
5.3.0 Se añadierons las opciones MYSQLI_OPT_INT_AND_FLOAT_NATIVE, MYSQLI_OPT_NET_CMD_BUFFER_SIZE, MYSQLI_OPT_NET_READ_BUFFER_SIZE, y MYSQLI_OPT_SSL_VERIFY_SERVER_CERT.

Ejemplos

Vea mysqli_real_connect().

Notas

Nota:

MySQLnd siempre utiliza el juego de caracteres de idioma predeterminado. El juego de caracteres se envía en la autentificación/acuerdo que se produce durante la conexión, que utilizará mysqlnd.

Libmysqlclient utiliza el juego de caracteres predeterminado definido en my.cnf o se puede establecer llamando mysqli_options() antes de user mysqli_real_connect(), pero después de mysqli_init().

Ver también

add a note

User Contributed Notes 6 notes

up
9
php at darkain dot com
5 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
1
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
Guy Sartorelli
10 months 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
1
puneetsharam9 at hotmail dot com
1 year 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
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