Commit 4188a491 authored by Alexander Butenko's avatar Alexander Butenko

Added ping() with reconnect abbility

parent 9b658126
...@@ -75,6 +75,18 @@ class MysqliDb ...@@ -75,6 +75,18 @@ class MysqliDb
* @var string * @var string
*/ */
protected $_stmtError; protected $_stmtError;
/**
* Database credentials
*
* @var string
*/
protected $host;
protected $username;
protected $password;
protected $db;
protected $port;
/** /**
* @param string $host * @param string $host
* @param string $username * @param string $username
...@@ -84,17 +96,30 @@ class MysqliDb ...@@ -84,17 +96,30 @@ class MysqliDb
*/ */
public function __construct($host, $username, $password, $db, $port = NULL) public function __construct($host, $username, $password, $db, $port = NULL)
{ {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
if($port == NULL) if($port == NULL)
$port = ini_get('mysqli.default_port'); $this->port = ini_get ('mysqli.default_port');
else
$this->_mysqli = new mysqli($host, $username, $password, $db, $port) $this->port = $port;
or die('There was a problem connecting to the database');
$this->_mysqli->set_charset('utf8');
$this->connect();
self::$_instance = $this; self::$_instance = $this;
} }
/**
* A method to connect to the database
*
*/
public function connect()
{
$this->_mysqli = new mysqli ($this->host, $this->username, $this->password, $this->db, $this->port)
or die('There was a problem connecting to the database');
$this->_mysqli->set_charset ('utf8');
}
/** /**
* A method of returning the static instance to allow access to the * A method of returning the static instance to allow access to the
* instantiated object from within another class. * instantiated object from within another class.
...@@ -388,6 +413,18 @@ class MysqliDb ...@@ -388,6 +413,18 @@ class MysqliDb
return $this->_mysqli->real_escape_string($str); return $this->_mysqli->real_escape_string($str);
} }
/**
* Method to call mysqli->ping() to keep unused connections open on
* long-running scripts, or to reconnect timed out connections (if php.ini has
* global mysqli.reconnect set to true). Can't do this directly using object
* since _mysqli is protected.
*
* @return bool True if connection is up
*/
public function ping() {
return $this->_mysqli->ping();
}
/** /**
* This method is needed for prepared statements. They require * This method is needed for prepared statements. They require
* the data type of the field to be bound with "i" s", etc. * the data type of the field to be bound with "i" s", etc.
......
...@@ -200,3 +200,15 @@ $db->where("u.id", 6); ...@@ -200,3 +200,15 @@ $db->where("u.id", 6);
$products = $db->get ("products p", null, "u.name, p.productName"); $products = $db->get ("products p", null, "u.name, p.productName");
print_r ($products); print_r ($products);
``` ```
### Helper commands
Reconnect in case mysql connection died
```php
if (!$db->ping())
$db->connect()
```
Obtain an initialized instance of the class from another class
```php
$db = MysqliDb::getInstance();
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment