PHP 8.4.0 RC2 available for testing

mysqli::ping

mysqli_ping

(PHP 5, PHP 7, PHP 8)

mysqli::ping -- mysqli_pingPing la connexion au serveur et reconnecte si elle n'existe plus

Description

Style orienté objet

public mysqli::ping(): bool

Style procédural

mysqli_ping(mysqli $mysql): bool

Vérifie si la connexion au serveur fonctionne correctement. Si elle a été refermée et que l'option globale mysqli.reconnect est activée, une reconnexion automatique est tentée.

Note: Le paramètre php.ini mysqli.reconnect est ignoré par le pilote mysqlnd, donc les reconnexions automatique ne sont jamais tentées.

Cette fonction peut être utilisée pour que les clients qui restent longtemps ouverts sans action puissent vérifier que la connexion n'a pas été refermée par le serveur, et, le cas échéant, faire une reconnexion automatique.

Liste de paramètres

mysql

Seulement en style procédural : Un objet mysqli retourné par la fonction mysqli_connect() ou mysqli_init().

Valeurs de retour

Cette fonction retourne true en cas de succès ou false si une erreur survient.

Erreurs / Exceptions

Si le rapport d'erreurs mysqli est activé (MYSQLI_REPORT_ERROR) et que l'opération demandée échoue, un avertissement est généré. Si, en plus, le mode est défini sur MYSQLI_REPORT_STRICT, une mysqli_sql_exception est lancée à la place.

Exemples

Exemple #1 Exemple avec mysqli::ping()

Style orienté objet

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

/* Vérification de la connexion */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}

/* Vérification si la connexion est toujours active */
if ($mysqli->ping()) {
printf ("La connexion est Ok !\n");
} else {
printf ("Erreur : %s\n", $mysqli->error);
}

/* Fermeture de la connexion */
$mysqli->close();
?>

Style procédural

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* Vérification de la connexion */
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}

/* Vérification si la connexion est toujours active */
if (mysqli_ping($link)) {
printf ("La connexion est Ok !\n");
} else {
printf ("Erreur : %s\n", mysqli_error($link));
}

/* Fermeture de la connexion */
mysqli_close($link);
?>

Les exemples ci-dessus vont afficher :

La connexion est valide !
add a note

User Contributed Notes 3 notes

up
14
jay at grooveshark dot com
10 years ago
This does not work with mysqlnd and is marked as wontfix: https://bugs.php.net/bug.php?id=52561
up
5
root at jusme dot org
9 years ago
As jay at grooveshark dot com very helpfully pointed out, the mysqlnd driver which is becoming pretty standard does not obey reconnect commands. If you have a DB wrapper class (which hopefully you do) you can implement your own version of ping() such as:

<?php

class db extends mysqli
{
private
$db_host;
private
$db_user;
private
$db_pass;
private
$db_name;
private
$persistent;

public function
__construct($db_host, $db_user, $db_pass, $db_name, $persistent = true)
{
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_name = $db_name;
$this->persistent = $persistent;

parent::init();
parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, 1);
@
parent::real_connect(($this->persistent ? 'p:' : '') . $this->db_host, $this->db_user, $this->db_pass, $this->db_name);

if (
$this->connect_errno)
die(
"All DB servers down!\n");
}

public function
ping()
{
@
parent::query('SELECT LAST_INSERT_ID()');

if (
$this->errno == 2006)
$this->__construct($this->db_host, $this->db_user, $this->db_pass, $this->db_name, $this->persistent);
}
...
}

$db = new db(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Some code that potentially takes a really long time to execute goes here
// Ping for safety to try to gracefully reconnect
$db->ping();
// Now we should be able to run queries again
$db->query('SELECT LAST_INSERT_ID()');

?>

If you wanted you could even put "$this->ping();" at the top of db::query() to avoid any explicit reconnection calls but I wouldn't recommend it due to the (slight) overhead of running the cheap "SELECT LAST_INSERT_ID()" query every time prior to running your real intended query. There are probably even cheaper queries to run in favor of "SELECT LAST_INSERT_ID()" but it was the first that came to mind and is cheap enough for most purposes since you shouldn't be calling ping() a whole bunch anyway.
up
4
snooops84 at googlemail dot com
9 years ago
The behaviour about the option mysqli.reconnect is default set to Off at Debian PHP Packages. So i would recommend to update the first line description about the recommendation at the option mysqli.reconnect. (practice note ;))
To Top