Commit 0e8a6d59 authored by Jesse Norell's avatar Jesse Norell

fix db connection test for server.php

parent 8b1b6ecc
......@@ -52,8 +52,7 @@ class db
private $dbClientFlags = 0; // MySQL Client falgs
/**#@-*/
public $show_error_messages = false; // false in server, true in interface
public $show_error_messages = false; // false in server, interface sets true when generating templates
/* old things - unused now ////
private $linkId = 0; // last result of mysqli_connect()
......@@ -80,7 +79,7 @@ class db
$this->dbUser = $user ? $user : $conf['db_user'];
$this->dbPass = $pass ? $pass : $conf['db_password'];
$this->dbCharset = $conf['db_charset'];
$this->dbClientFlags = $flags ? $flags : $conf['db_client_flags'];
$this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags'];
$this->_iConnId = mysqli_init();
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
......@@ -117,6 +116,18 @@ class db
$this->_iConnId = null;
}
/*
* Test mysql connection.
*
* @return boolean returns true if db connection is good.
*/
public function testConnection() {
if(mysqli_connect_errno()) {
return false;
}
return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId));
}
/* This allows our private variables to be "read" out side of the class */
public function __get($var) {
return isset($this->$var) ? $this->$var : NULL;
......@@ -435,13 +446,14 @@ class db
* @return int id of last inserted row or 0 if none
*/
public function insert_id() {
$iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`');
if(!is_object($iRes)) return false;
$aReturn = mysqli_fetch_assoc($iRes);
mysqli_free_result($iRes);
return $aReturn['newid'];
$oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`');
if(!$oResult) {
$this->_sqlerror('Unable to select last_insert_id()');
return false;
}
$aReturn = $oResult->get();
$oResult->free();
return isset($aReturn['newid']) ? $aReturn['newid'] : 0;
}
......
......@@ -52,8 +52,7 @@ class db
private $dbClientFlags = 0; // MySQL Client falgs
/**#@-*/
public $show_error_messages = false; // false in server, true in interface
public $show_error_messages = false; // false in server, interface sets true when generating templates
/* old things - unused now ////
private $linkId = 0; // last result of mysqli_connect()
......@@ -80,7 +79,7 @@ class db
$this->dbUser = $user ? $user : $conf['db_user'];
$this->dbPass = $pass ? $pass : $conf['db_password'];
$this->dbCharset = $conf['db_charset'];
$this->dbClientFlags = $flags ? $flags : $conf['db_client_flags'];
$this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags'];
$this->_iConnId = mysqli_init();
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
......@@ -117,6 +116,18 @@ class db
$this->_iConnId = null;
}
/*
* Test mysql connection.
*
* @return boolean returns true if db connection is good.
*/
public function testConnection() {
if(mysqli_connect_errno()) {
return false;
}
return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId));
}
/* This allows our private variables to be "read" out side of the class */
public function __get($var) {
return isset($this->$var) ? $this->$var : NULL;
......@@ -435,13 +446,14 @@ class db
* @return int id of last inserted row or 0 if none
*/
public function insert_id() {
$iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`');
if(!is_object($iRes)) return false;
$aReturn = mysqli_fetch_assoc($iRes);
mysqli_free_result($iRes);
return $aReturn['newid'];
$oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`');
if(!$oResult) {
$this->_sqlerror('Unable to select last_insert_id()');
return false;
}
$aReturn = $oResult->get();
$oResult->free();
return isset($aReturn['newid']) ? $aReturn['newid'] : 0;
}
......
......@@ -61,7 +61,7 @@ $conf['server_id'] = intval($conf['server_id']);
/*
* Try to Load the server configuration from the master-db
*/
if ($app->dbmaster->connect_error == NULL) {
if ($app->dbmaster->testConnection()) {
$server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
if(!is_array($server_db_record)) die('Unable to load the server configuration from database.');
......@@ -152,7 +152,7 @@ $needStartCore = true;
/*
* Next we try to process the datalog
*/
if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
if ($app->db->testConnection() && $app->dbmaster->testConnection()) {
// Check if there is anything to update
if ($conf['mirror_server_id'] > 0) {
......@@ -187,7 +187,7 @@ if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
$needStartCore = false;
} else {
if ($app->db->connect->connect_error == NULL) {
if (!$app->db->connect->testConnection()) {
$app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
} else {
$app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);
......
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