Should I check MySQL connection with is_resource or mysqli_ping?

Should I check MySQL connection with is_resourceor mysqli_ping? Why?

if (!mysql_ping($mysqli)) {
    $mysqli = new mysqli($db_host,
                         $db_username,
                         $db_password,
                         $db_name);
}

if (!is_resource($mysqli)) {
    $mysqli = new mysqli($db_host,
                         $db_username,
                         $db_password,
                         $db_name);
}
+4
source share
2 answers

is_resource($mysqli) does not report the MySQLi resource as a valid resource type.

MySQLi uses objects as a storage container, not resources; even when using the procedural API, so exit is expected.

It was like a mistake in the past , and it is not a mistake, it is the expected behavior, as Johannes states in the comment.

So, I think you should use mysqli_pingin the end.

+1
source

, "-" imo. , " ".

:
1) script $mysqli = new mysqli(... , valid, $mysqli - , . , $mysqli = mysqli_connect(..., null . $mysqli instanceof MySQLi, , is_resource().

2) mysql_ * MySQLi api. , mysql i _ping mysql_ping.


a) $mysqli instanceof MySQLi, , script mysqli, , . $mysqli MySQLi, script mysqli_ * MySQLi:: *, mysqli_ping. $mysqli=mysqli_connect() $mysqli = new mysqli(...) $mysqli NULL. instanceof "" false . , $mysqli - ... - , MySQLi.

b) MySQLi, mysqli_ping($mysqli) $mysqli->ping(), , . MySQLi , , , , .

+1

All Articles