Skip to content

Commit 0e8a6d5

Browse files
committed
fix db connection test for server.php
1 parent 8b1b6ec commit 0e8a6d5

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

interface/lib/classes/db_mysql.inc.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class db
5252
private $dbClientFlags = 0; // MySQL Client falgs
5353
/**#@-*/
5454

55-
public $show_error_messages = false; // false in server, true in interface
56-
55+
public $show_error_messages = false; // false in server, interface sets true when generating templates
5756

5857
/* old things - unused now ////
5958
private $linkId = 0; // last result of mysqli_connect()
@@ -80,7 +79,7 @@ public function __construct($host = NULL , $user = NULL, $pass = NULL, $database
8079
$this->dbUser = $user ? $user : $conf['db_user'];
8180
$this->dbPass = $pass ? $pass : $conf['db_password'];
8281
$this->dbCharset = $conf['db_charset'];
83-
$this->dbClientFlags = $flags ? $flags : $conf['db_client_flags'];
82+
$this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags'];
8483
$this->_iConnId = mysqli_init();
8584

8685
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
@@ -117,6 +116,18 @@ public function close() {
117116
$this->_iConnId = null;
118117
}
119118

119+
/*
120+
* Test mysql connection.
121+
*
122+
* @return boolean returns true if db connection is good.
123+
*/
124+
public function testConnection() {
125+
if(mysqli_connect_errno()) {
126+
return false;
127+
}
128+
return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId));
129+
}
130+
120131
/* This allows our private variables to be "read" out side of the class */
121132
public function __get($var) {
122133
return isset($this->$var) ? $this->$var : NULL;
@@ -435,13 +446,14 @@ public function query_all_array($sQuery = '') {
435446
* @return int id of last inserted row or 0 if none
436447
*/
437448
public function insert_id() {
438-
$iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`');
439-
if(!is_object($iRes)) return false;
440-
441-
$aReturn = mysqli_fetch_assoc($iRes);
442-
mysqli_free_result($iRes);
443-
444-
return $aReturn['newid'];
449+
$oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`');
450+
if(!$oResult) {
451+
$this->_sqlerror('Unable to select last_insert_id()');
452+
return false;
453+
}
454+
$aReturn = $oResult->get();
455+
$oResult->free();
456+
return isset($aReturn['newid']) ? $aReturn['newid'] : 0;
445457
}
446458

447459

server/lib/classes/db_mysql.inc.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class db
5252
private $dbClientFlags = 0; // MySQL Client falgs
5353
/**#@-*/
5454

55-
public $show_error_messages = false; // false in server, true in interface
56-
55+
public $show_error_messages = false; // false in server, interface sets true when generating templates
5756

5857
/* old things - unused now ////
5958
private $linkId = 0; // last result of mysqli_connect()
@@ -80,7 +79,7 @@ public function __construct($host = NULL , $user = NULL, $pass = NULL, $database
8079
$this->dbUser = $user ? $user : $conf['db_user'];
8180
$this->dbPass = $pass ? $pass : $conf['db_password'];
8281
$this->dbCharset = $conf['db_charset'];
83-
$this->dbClientFlags = $flags ? $flags : $conf['db_client_flags'];
82+
$this->dbClientFlags = ($flags !== NULL) ? $flags : $conf['db_client_flags'];
8483
$this->_iConnId = mysqli_init();
8584

8685
mysqli_real_connect($this->_iConnId, $this->dbHost, $this->dbUser, $this->dbPass, '', (int)$this->dbPort, NULL, $this->dbClientFlags);
@@ -117,6 +116,18 @@ public function close() {
117116
$this->_iConnId = null;
118117
}
119118

119+
/*
120+
* Test mysql connection.
121+
*
122+
* @return boolean returns true if db connection is good.
123+
*/
124+
public function testConnection() {
125+
if(mysqli_connect_errno()) {
126+
return false;
127+
}
128+
return (boolean)(is_object($this->_iConnId) && mysqli_ping($this->_iConnId));
129+
}
130+
120131
/* This allows our private variables to be "read" out side of the class */
121132
public function __get($var) {
122133
return isset($this->$var) ? $this->$var : NULL;
@@ -435,13 +446,14 @@ public function query_all_array($sQuery = '') {
435446
* @return int id of last inserted row or 0 if none
436447
*/
437448
public function insert_id() {
438-
$iRes = mysqli_query($this->_iConnId, 'SELECT LAST_INSERT_ID() as `newid`');
439-
if(!is_object($iRes)) return false;
440-
441-
$aReturn = mysqli_fetch_assoc($iRes);
442-
mysqli_free_result($iRes);
443-
444-
return $aReturn['newid'];
449+
$oResult = $this->query('SELECT LAST_INSERT_ID() as `newid`');
450+
if(!$oResult) {
451+
$this->_sqlerror('Unable to select last_insert_id()');
452+
return false;
453+
}
454+
$aReturn = $oResult->get();
455+
$oResult->free();
456+
return isset($aReturn['newid']) ? $aReturn['newid'] : 0;
445457
}
446458

447459

server/server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
/*
6262
* Try to Load the server configuration from the master-db
6363
*/
64-
if ($app->dbmaster->connect_error == NULL) {
64+
if ($app->dbmaster->testConnection()) {
6565
$server_db_record = $app->dbmaster->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']);
6666

6767
if(!is_array($server_db_record)) die('Unable to load the server configuration from database.');
@@ -152,7 +152,7 @@
152152
/*
153153
* Next we try to process the datalog
154154
*/
155-
if ($app->db->connect_error == NULL && $app->dbmaster->connect_error == NULL) {
155+
if ($app->db->testConnection() && $app->dbmaster->testConnection()) {
156156

157157
// Check if there is anything to update
158158
if ($conf['mirror_server_id'] > 0) {
@@ -187,7 +187,7 @@
187187
$needStartCore = false;
188188

189189
} else {
190-
if ($app->db->connect->connect_error == NULL) {
190+
if (!$app->db->connect->testConnection()) {
191191
$app->log('Unable to connect to local server.' . $app->db->errorMessage, LOGLEVEL_WARN);
192192
} else {
193193
$app->log('Unable to connect to master server.' . $app->dbmaster->errorMessage, LOGLEVEL_WARN);

0 commit comments

Comments
 (0)