@@ -45,6 +45,7 @@ class db extends mysqli
4545 public $ errorMessage = '' ; // last error message
4646 private $ errorLocation = '' ;// last error location
4747 public $ show_error_messages = true ; // false in server, true in interface
48+ private $ isConnected = false ; // needed to know if we have a valid mysqli object from the constructor
4849
4950 // constructor
5051 public function __construct ($ prefix = '' ) {
@@ -58,12 +59,20 @@ public function __construct($prefix = '') {
5859 $ this ->dbNewLink = $ conf [$ prefix .'db_new_link ' ];
5960 $ this ->dbClientFlags = $ conf [$ prefix .'db_client_flags ' ];
6061 parent ::__construct ($ conf [$ prefix .'db_host ' ], $ conf [$ prefix .'db_user ' ],$ conf [$ prefix .'db_password ' ],$ conf [$ prefix .'db_database ' ]);
61- if ($ this ->connect_error ) {
62+ $ try = 0 ;
63+ while (!is_null ($ this ->connect_error ) && $ try < 5 ) {
64+ if ($ try > 0 ) sleep (1 );
65+
66+ $ try ++;
6267 $ this ->updateError ('DB::__construct ' );
63- return false ;
64- } else {
65- $ this ->setCharacterEncoding ();
68+
69+ parent ::__construct ($ conf [$ prefix .'db_host ' ], $ conf [$ prefix .'db_user ' ],$ conf [$ prefix .'db_password ' ],$ conf [$ prefix .'db_database ' ]);
6670 }
71+
72+ if (is_null ($ this ->connect_error )) $ this ->isConnected = true ;
73+ else return false ;
74+
75+ $ this ->setCharacterEncoding ();
6776 }
6877
6978 public function __destruct () {
@@ -74,7 +83,7 @@ public function __destruct() {
7483 public function updateError ($ location ) {
7584 global $ app ;
7685
77- if ($ this ->connect_error ) {
86+ if (! is_null ( $ this ->connect_error ) ) {
7887 $ this ->errorNumber = $ this ->connect_errno ;
7988 $ this ->errorMessage = $ this ->connect_error ;
8089 } else {
@@ -95,18 +104,20 @@ public function updateError($location) {
95104 }
96105
97106 private function setCharacterEncoding () {
107+ if ($ this ->isConnected == false ) return false ;
98108 parent ::query ( 'SET NAMES ' .$ this ->dbCharset );
99109 parent ::query ( "SET character_set_results = ' " .$ this ->dbCharset ."', character_set_client = ' " .$ this ->dbCharset ."', character_set_connection = ' " .$ this ->dbCharset ."', character_set_database = ' " .$ this ->dbCharset ."', character_set_server = ' " .$ this ->dbCharset ."' " );
100110 }
101111
102112 public function query ($ queryString ) {
113+ if ($ this ->isConnected == false ) return false ;
103114 $ try = 0 ;
104115 do {
105116 $ try ++;
106- $ ok = parent :: ping ();
117+ $ ok = $ this -> ping ();
107118 if (!$ ok ) {
108- if (!parent :: real_connect ($ this ->dbHost , $ this ->dbUser , $ this ->dbPass ,$ this ->dbName )) {
109- if ($ try > 9 ) {
119+ if (!$ this -> real_connect ($ this ->dbHost , $ this ->dbUser , $ this ->dbPass , $ this ->dbName )) {
120+ if ($ try > 4 ) {
110121 $ this ->updateError ('DB::query -> reconnect ' );
111122 return false ;
112123 } else {
@@ -479,7 +490,7 @@ public function dropTable($table_name) {
479490
480491 // gibt Array mit Tabellennamen zur�ck
481492 public function getTables ($ database_name = '' ) {
482-
493+ if ( $ this -> isConnected == false ) return false ;
483494 if ($ database_name == '' ) $ database_name = $ this ->dbName ;
484495 $ result = parent ::query ("SHOW TABLES FROM $ database_name " );
485496 for ($ i = 0 ; $ i < $ result ->num_rows ; $ i ++) {
0 commit comments