PHP mysqli real_connect() Functie
Voorbeeld - Objectgeoriënteerde stijl
Open een nieuwe verbinding met de MySQL-server, met extra verbindingsopties:
<?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");
?>
Kijk naar voorbeeld van procedurele stijl onderaan.
Definitie en gebruik
De functie real_connect() / mysqli_real_connect() opent een nieuwe verbinding met de MySQL-server.
Deze functie verschilt van connect() op de volgende manieren:
- real_connect() vereist een geldig object gemaakt door init()
- real_connect() kan worden gebruikt met options() om verschillende opties voor de verbinding in te stellen
- real_connect() heeft een vlagparameter
Syntaxis
Objectgeoriënteerde stijl:
$mysqli ->
real_connect(host, username, password, dbname, port, socket, flag)
Procedurele stijl:
mysqli_real_connect(connection, host, username, password, dbname, port, socket, flag)
Parameterwaarden
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
Winstwaarde: | WAAR op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5+ |
PHP-wijzigingslogboek: | PHP 5.6: MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT-vlag toegevoegd |
Voorbeeld - Procedurele stijl
Open een nieuwe verbinding met de MySQL-server, met extra verbindingsopties:
<?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-referentie