PHP 8.3.4 Released!

mysqli::options

mysqli_options

(PHP 5, PHP 7, PHP 8)

mysqli::options -- mysqli_optionsDéfinit les options

Description

Style orienté objet

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

Style procédural

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

Utile pour définir des options de connexion et ainsi affecter le comportement de la connexion courante.

Cette fonction peut être appelée plusieurs fois pour définir plusieurs options.

mysqli_options() doit être appelée après mysqli_init() et avant mysqli_real_connect().

Liste de paramètres

mysql

Seulement en style procédural : Un objet mysqli retourné par la fonction mysqli_connect() ou mysqli_init().

option

L'option que vous voulez définir. Il peut prendre une des valeurs suivantes :

Options valides
Nom Description
MYSQLI_OPT_CONNECT_TIMEOUT Délai maximal de la connexion en secondes
MYSQLI_OPT_READ_TIMEOUT Résultat d'execution durée d'expiration d'une commande en secondes. Disponible à partir de PHP 7.2.0.
MYSQLI_OPT_LOCAL_INFILE Active/désactive l'utilisation de LOAD LOCAL INFILE
MYSQLI_INIT_COMMAND Commande à exécuter après la connexion au serveur MySQL
MYSQLI_SET_CHARSET_NAME Le jeu de caractère à définir par défaut.
MYSQLI_READ_DEFAULT_FILE Lit les options depuis le nom de l'option plutôt que du fichier my.cnf Pas supporté par mysqlnd
MYSQLI_READ_DEFAULT_GROUP Lit les options du groupe depuis my.cnf ou depuis le fichier spécifié avec MYSQL_READ_DEFAULT_FILE. Pas supporté par mysqlnd
MYSQLI_SERVER_PUBLIC_KEY Fichier contenant la clé publique RSA utilisée avec l'authentification basée sur SHA-256.
MYSQLI_OPT_NET_CMD_BUFFER_SIZE La taille du buffer interne de commande/réseau. Uniquement valide pour mysqlnd.
MYSQLI_OPT_NET_READ_BUFFER_SIZE Taille, en octets, maximal de la partie à lire lors de la lecture du corps d'un paquet de commande MySQL. Uniquement valide pour mysqlnd.
MYSQLI_OPT_INT_AND_FLOAT_NATIVE Convertie les colonnes entières et à virgules flottantes en nombres PHP lors de l'utilisation de déclarations non préparées. Valable uniquement pour mysqlnd.
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT Si le certificat de serveur doit être vérifier ou non.

value

La valeur pour l'option.

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Erreurs / Exceptions

Si le rapport d'erreurs mysqli est activé (MYSQLI_REPORT_ERROR) et que l'opération demandée échoue, un avertissement est généré. Si, en plus, le mode est défini sur MYSQLI_REPORT_STRICT, une mysqli_sql_exception est lancée à la place.

Exemples

Voir mysqli_real_connect().

Notes

Note:

MySQLnd s'occupe toujours du jeu de caractères par défaut du serveur. Celui-ci est envoyé durant la négociation de la connexion ou l'authentification.

Libmysqlclient utilise le jeu de caractères par défaut de my.cnf ou via par un appel à mysqli_options() avant mysqli_real_connect(), mais après mysqli_init().

Voir aussi

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