0

Im using this class https://github.com/joshcam/PHP-MySQLi-Database-Class (MysqliDb.php) which works fine however i cant determine if the connection to mysql closes automatically or if i need to close the connection somehow at the end of my file(s) that are using the class.

How to check if the connection is closed or has closed?

Ynhockey
  • 3,845
  • 5
  • 33
  • 51
user520300
  • 1,497
  • 5
  • 24
  • 46
  • From PHP.net: Open non-persistent MySQL connections and result sets are automatically destroyed when a PHP script finishes its execution. So, while explicitly closing open connections and freeing result sets is optional, doing so is recommended. – sanderbee Jan 15 '16 at 20:10

2 Answers2

0

There is a standard php function mysqli_ping: Pings a server connection, or tries to reconnect if the connection has gone down.

In the library this is ping().

if (!$db->ping())
    $db->connect()

Look in the documentation.

Volodymyr Chumak
  • 756
  • 6
  • 12
0

The mysqli class has a method to this:

 bool mysqli::get_connection_stats ( void )

http://php.net/manual/en/mysqli.get-connection-stats.php

To use with this class you need get the mysqli instance:

$db = new MysqliDb;
$instance = $db->mysqli();
$connStat = $instance->get_connection_stats();
var_dump($connStat);

This class that you are using close the connection automatically when destroyed through the magic method __destruct()

http://php.net/manual/en/language.oop5.decon.php#object.destruct