@@ -64,8 +64,8 @@ class db
6464 private $record = array(); // last record fetched
6565 private $autoCommit = 1; // Autocommit Transactions
6666 private $currentRow; // current row number
67- public $errorNumber = 0; // last error number
6867 */
68+ public $ errorNumber = 0 ; // last error number
6969 public $ errorMessage = '' ; // last error message
7070 /*
7171 private $errorLocation = '';// last error location
@@ -94,18 +94,20 @@ public function __construct($host = NULL , $user = NULL, $pass = NULL, $database
9494 if (!is_object ($ this ->_iConnId )) {
9595 $ this ->_iConnId = mysqli_init ();
9696 }
97- mysqli_real_connect ($ this ->_iConnId , $ this ->dbHost , $ this ->dbUser , $ this ->dbPass , '' , (int )$ this ->dbPort , NULL , $ this ->dbClientFlags );
97+ if (!mysqli_real_connect ($ this ->_iConnId , $ this ->dbHost , $ this ->dbUser , $ this ->dbPass , '' , (int )$ this ->dbPort , NULL , $ this ->dbClientFlags )) {
98+ $ this ->_sqlerror ('Database connection failed ' );
99+ }
98100 }
99101
100102 if (!is_object ($ this ->_iConnId ) || mysqli_connect_errno ()) {
101103 $ this ->_iConnId = null ;
102- $ this ->_sqlerror ('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible! ' , '' , true );
103- return false ;
104+ $ this ->_sqlerror ('Zugriff auf Datenbankserver fehlgeschlagen! / Database server not accessible! ' , '' , true ); // sets errorMessage
105+ throw new Exception ( $ this -> errorMessage ) ;
104106 }
105107 if (!((bool )mysqli_query ( $ this ->_iConnId , 'USE ` ' . $ this ->dbName . '` ' ))) {
106108 $ this ->close ();
107- $ this ->_sqlerror ('Datenbank nicht gefunden / Database not found ' , '' , true );
108- return false ;
109+ $ this ->_sqlerror ('Datenbank nicht gefunden / Database not found ' , '' , true ); // sets errorMessage
110+ throw new Exception ( $ this -> errorMessage ) ;
109111 }
110112
111113 $ this ->_setCharset ();
@@ -261,8 +263,11 @@ private function _query($sQuery = '') {
261263 $ try ++;
262264 $ ok = (is_object ($ this ->_iConnId )) ? mysqli_ping ($ this ->_iConnId ) : false ;
263265 if (!$ ok ) {
264- if (!mysqli_real_connect (mysqli_init (), $ this ->dbHost , $ this ->dbUser , $ this ->dbPass , $ this ->dbName , (int )$ this ->dbPort , NULL , $ this ->dbClientFlags )) {
265- if ($ this ->errorNumber == '111 ' ) {
266+ if (!is_object ($ this ->_iConnId )) {
267+ $ this ->_iConnId = mysqli_init ();
268+ }
269+ if (!mysqli_real_connect ($ this ->_isConnId , $ this ->dbHost , $ this ->dbUser , $ this ->dbPass , $ this ->dbName , (int )$ this ->dbPort , NULL , $ this ->dbClientFlags )) {
270+ if (mysqli_connect_errno () == '111 ' ) {
266271 // server is not available
267272 if ($ try > 9 ) {
268273 if (isset ($ app ) && isset ($ app ->forceErrorExit )) {
@@ -531,8 +536,13 @@ public function escape($sString) {
531536 private function _sqlerror ($ sErrormsg = 'Unbekannter Fehler ' , $ sAddMsg = '' , $ bNoLog = false ) {
532537 global $ app , $ conf ;
533538
534- $ mysql_error = (is_object ($ this ->_iConnId ) ? mysqli_error ($ this ->_iConnId ) : mysqli_connect_error ());
535- $ mysql_errno = (is_object ($ this ->_iConnId ) ? mysqli_errno ($ this ->_iConnId ) : mysqli_connect_errno ());
539+ $ mysql_errno = mysqli_connect_errno ();
540+ $ mysql_error = mysqli_connect_error ();
541+ if ($ mysql_errno === 0 && is_object ($ this ->_iConnId )) {
542+ $ mysql_errno = mysqli_errno ($ this ->_iConnId );
543+ $ mysql_error = mysqli_error ($ this ->_iConnId );
544+ }
545+ $ this ->errorNumber = $ mysql_error ;
536546 $ this ->errorMessage = $ mysql_error ;
537547
538548 //$sAddMsg .= getDebugBacktrace();
0 commit comments