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.
(PHP 5, PHP 7, PHP 8)
mysqli::options -- mysqli_options — Setzt Optionen
Objektorientierter Stil
Prozeduraler Stil
Wird verwendet, um zusätzliche Verbindungsoptionen festzulegen und das Verhalten einer Verbindung zu beeinflussen.
Diese Funktion kann mehrfach aufgerufen werden, um mehrere Optionen zu setzen.
mysqli_options() muss nach mysqli_init() und vor mysqli_real_connect() aufgerufen werden.
mysql
Nur bei prozeduralem Aufruf: ein von mysqli_connect() oder mysqli_init() zurückgegebenes mysqli-Objekt.
option
Die Option, die gesetzt werden soll. Es kann einer der folgenden Werte sein:
Name | Beschreibung |
---|---|
MYSQLI_OPT_CONNECT_TIMEOUT |
Zeitlimit für den Verbindungsaufbau (in Sekunden) |
MYSQLI_OPT_READ_TIMEOUT |
Zeitlimit für die Ausführung eines Befehls (in Sekunden). Verfügbar seit PHP 7.2.0. |
MYSQLI_OPT_LOCAL_INFILE |
Aktivieren/Deaktivieren von LOAD LOCAL INFILE |
MYSQLI_INIT_COMMAND |
Der Befehl, der nach dem Aufbau einer Verbindung zum MySQL-Server ausgeführt wird |
MYSQLI_SET_CHARSET_NAME |
Der Zeichensatz, der als Standard gesetzt werden soll |
MYSQLI_READ_DEFAULT_FILE |
Optionen aus der benannten Optionsdatei lesen statt aus my.cnf; wird von mysqlnd nicht unterstützt. |
MYSQLI_READ_DEFAULT_GROUP |
Liest die Optionen der benannten Gruppe aus der Datei
my.cnf oder der Datei, die mit
MYSQL_READ_DEFAULT_FILE angegeben wurde;
wird von mysqlnd nicht unterstützt.
|
MYSQLI_SERVER_PUBLIC_KEY |
Die öffentliche RSA-Schlüsseldatei, die für die SHA-256-basierte Authentifizierung verwendet wird. |
MYSQLI_OPT_NET_CMD_BUFFER_SIZE |
Die Größe des internen Befehls-/Netzwerkpuffers; gilt nur für mysqlnd. |
MYSQLI_OPT_NET_READ_BUFFER_SIZE |
Maximale zu lesende Blockgröße (in Bytes), wenn der Textkörper eines MySQL-Befehlspakets gelesen wird; gilt nur für mysqlnd. |
MYSQLI_OPT_INT_AND_FLOAT_NATIVE |
Wenn nicht-vorbereitete Anweisungen verwendet werden, werden Spalten mit Integer- und Float-Werten wieder in PHP-Zahlen umgewandelt; gilt nur für mysqlnd. |
MYSQLI_OPT_SSL_VERIFY_SERVER_CERT |
Gibt an, ob das Serverzertifikat verifiziert werden soll oder nicht. |
value
Der Wert für die Option
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.
Siehe mysqli_real_connect().
Hinweis:
MySQLnd nimmt immer den Standardzeichensatz des Servers an. Dieser Zeichensatz wird während des Aufbaus der Verbindung bzw. der Authentifizierung übermittelt und danach von MySQLnd verwendet.
Libmysqlclient verwendet als Standardzeichensatz den, der in der Datei my.cnf angegeben oder durch einen Aufruf von mysqli_options() vor dem Aufruf von mysqli_real_connect() aber nach mysqli_init() gesetzt wurde.
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.
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');
?>
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();
?>
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);
}
}
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.
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;
}