Fonction PHP mysqli real_connect()
Exemple - Style orienté objet
Ouvrez une nouvelle connexion au serveur MySQL, avec des options de connexion supplémentaires :
<?php
$mysqli = mysqli_init();
if (!$mysqli) {
die("mysqli_init failed");
}
// Specify connection timeout
$con -> options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
// Specify read options from named file instead of my.cnf
$con ->
options(MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");
$con -> real_connect("localhost","my_user","my_password","my_db");
?>
Regardez l'exemple de style procédural en bas.
Définition et utilisation
La fonction real_connect() / mysqli_real_connect() ouvre une nouvelle connexion au serveur MySQL.
Cette fonction diffère de connect() des manières suivantes :
- real_connect() nécessite un objet valide créé par init()
- real_connect() peut être utilisé avec options() pour définir différentes options pour la connexion
- real_connect() a un paramètre flag
Syntaxe
Style orienté objet :
$mysqli ->
real_connect(host, username, password, dbname, port, socket, flag)
Style procédural :
mysqli_real_connect(connection, host, username, password, dbname, port, socket, flag)
Valeurs des paramètres
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
host | Optional. Specifies a host name or an IP address |
username | Optional. Specifies the MySQL username |
password | Optional. Specifies the MySQL password |
dbname | Optional. Specifies the default database to be used |
port | Optional. Specifies the port number to attempt to connect to the MySQL server |
socket | Optional. Specifies the socket or named pipe to be used |
flag | Optional. Specifies different connection options. Possible values:
|
Détails techniques
Valeur de retour : | VRAI en cas de succès. FAUX en cas d'échec |
---|---|
Version PHP : | 5+ |
Journal des modifications PHP : | PHP 5.6 : indicateur MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT ajouté |
Exemple - Style procédural
Ouvrez une nouvelle connexion au serveur MySQL, avec des options de connexion supplémentaires :
<?php
$con = mysqli_init();
if (!$con) {
die("mysqli_init failed");
}
// Specify connection timeout
mysqli_options($con,
MYSQLI_OPT_CONNECT_TIMEOUT, 10);
// Specify read options from named file instead of my.cnf
mysqli_options($con, MYSQLI_READ_DEFAULT_FILE, "myfile.cnf");
mysqli_real_connect($con,"localhost","my_user","my_password","my_db");
?>
❮ Référence PHP MySQLi