PHP mysqli ping() Functie
Voorbeeld - Objectgeoriënteerde stijl
Ping een serververbinding:
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
// Check connection
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
// Check if server is alive
if ($mysqli -> ping()) {
echo "Connection is ok!";
} else {
echo "Error: ".
$mysqli -> error);
}
$mysqli -> close();
?>
Kijk naar voorbeeld van procedurele stijl onderaan.
Definitie en gebruik
De functie ping() / mysqli_ping() pingt een serververbinding om te controleren of de server actief is. Het probeert ook opnieuw verbinding te maken - als de verbinding is verbroken.
Syntaxis
Objectgeoriënteerde stijl:
$mysqli -> ping()
Procedurele stijl:
mysqli_ping(connection)
Parameterwaarden
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
Technische details
Winstwaarde: | WAAR op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5+ |
Voorbeeld - Procedurele stijl
Ping een serververbinding:
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
// Check if server is alive
if (mysqli_ping($con)) {
echo "Connection is ok!";
} else {
echo "Error: ". mysqli_error($con);
}
mysqli_close($con);
?>
❮ PHP MySQLi-referentie