PHP mysqli real_connect() Funktion
Beispiel – Objektorientierter Stil
Öffnen Sie eine neue Verbindung zum MySQL-Server mit zusätzlichen Verbindungsoptionen:
<?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");
?>
Sehen Sie sich das Beispiel für den prozeduralen Stil unten an.
Definition und Verwendung
Die Funktion real_connect() / mysqli_real_connect() öffnet eine neue Verbindung zum MySQL-Server.
Diese Funktion unterscheidet sich von connect() in folgenden Punkten:
- real_connect() erfordert ein gültiges Objekt, das von init() erstellt wurde
- real_connect() kann mit options() verwendet werden, um verschiedene Optionen für die Verbindung festzulegen
- real_connect() hat einen Flag-Parameter
Syntax
Objektorientierter Stil:
$mysqli ->
real_connect(host, username, password, dbname, port, socket, flag)
Verfahrensstil:
mysqli_real_connect(connection, host, username, password, dbname, port, socket, flag)
Parameterwerte
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:
|
Technische Details
Rückgabewert: | TRUE auf Erfolg. FALSE bei Fehler |
---|---|
PHP-Version: | 5+ |
PHP-Änderungsprotokoll: | PHP 5.6: Flag MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT hinzugefügt |
Beispiel – prozeduraler Stil
Öffnen Sie eine neue Verbindung zum MySQL-Server mit zusätzlichen Verbindungsoptionen:
<?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");
?>
❮ PHP MySQLi-Referenz