@@ -167,11 +167,6 @@ public function __construct($host = NULL, $username = NULL, $password = NULL, $d
167167 $ this ->isSubQuery = true ;
168168 return ;
169169 }
170-
171- // for subqueries we do not need database connection and redefine root instance
172- if (!is_object ($ host ))
173- $ this ->connect ();
174-
175170 $ this ->setPrefix ();
176171 self ::$ _instance = $ this ;
177172 }
@@ -192,8 +187,19 @@ public function connect()
192187 or die ('There was a problem connecting to the database ' );
193188
194189 if ($ this ->charset )
195- $ this ->_mysqli ->set_charset ($ this ->charset );
190+ $ this ->mysqli ()->set_charset ($ this ->charset );
191+ }
192+
193+ /**
194+ * A method to get mysqli object or create it in case needed
195+ */
196+ public function mysqli ()
197+ {
198+ if (!$ this ->_mysqli )
199+ $ this ->connect ();
200+ return $ this ->_mysqli ;
196201 }
202+
197203 /**
198204 * A method of returning the static instance to allow access to the
199205 * instantiated object from within another class.
@@ -643,7 +649,7 @@ public function groupBy($groupByField)
643649 */
644650 public function getInsertId ()
645651 {
646- return $ this ->_mysqli ->insert_id ;
652+ return $ this ->mysqli () ->insert_id ;
647653 }
648654
649655 /**
@@ -655,7 +661,7 @@ public function getInsertId()
655661 */
656662 public function escape ($ str )
657663 {
658- return $ this ->_mysqli ->real_escape_string ($ str );
664+ return $ this ->mysqli () ->real_escape_string ($ str );
659665 }
660666
661667 /**
@@ -667,7 +673,7 @@ public function escape($str)
667673 * @return bool True if connection is up
668674 */
669675 public function ping () {
670- return $ this ->_mysqli ->ping ();
676+ return $ this ->mysqli () ->ping ();
671677 }
672678
673679 /**
@@ -876,11 +882,11 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
876882 array_push ($ results , $ x );
877883 }
878884 // stored procedures sometimes can return more then 1 resultset
879- if ($ this ->_mysqli ->more_results ())
880- $ this ->_mysqli ->next_result ();
885+ if ($ this ->mysqli () ->more_results ())
886+ $ this ->mysqli () ->next_result ();
881887
882888 if (in_array ('SQL_CALC_FOUND_ROWS ' , $ this ->_queryOptions )) {
883- $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS() ' );
889+ $ stmt = $ this ->mysqli () ->query ('SELECT FOUND_ROWS() ' );
884890 $ totalCount = $ stmt ->fetch_row ();
885891 $ this ->totalCount = $ totalCount [0 ];
886892 }
@@ -1074,8 +1080,8 @@ protected function _buildLimit ($numRows) {
10741080 */
10751081 protected function _prepareQuery ()
10761082 {
1077- if (!$ stmt = $ this ->_mysqli ->prepare ($ this ->_query )) {
1078- trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->_mysqli ->error , E_USER_ERROR );
1083+ if (!$ stmt = $ this ->mysqli () ->prepare ($ this ->_query )) {
1084+ trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->mysqli () ->error , E_USER_ERROR );
10791085 }
10801086 if ($ this ->traceEnabled )
10811087 $ this ->traceStartQ = microtime (true );
@@ -1091,7 +1097,7 @@ public function __destruct()
10911097 if (!$ this ->isSubQuery )
10921098 return ;
10931099 if ($ this ->_mysqli )
1094- $ this ->_mysqli ->close ();
1100+ $ this ->mysqli () ->close ();
10951101 }
10961102
10971103 /**
@@ -1151,7 +1157,9 @@ public function getLastQuery () {
11511157 * @return string
11521158 */
11531159 public function getLastError () {
1154- return trim ($ this ->_stmtError . " " . $ this ->_mysqli ->error );
1160+ if (!$ this ->_mysqli )
1161+ return "mysqli is null " ;
1162+ return trim ($ this ->_stmtError . " " . $ this ->mysqli ()->error );
11551163 }
11561164
11571165 /**
@@ -1276,7 +1284,7 @@ public function copy ()
12761284 * @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
12771285 */
12781286 public function startTransaction () {
1279- $ this ->_mysqli ->autocommit (false );
1287+ $ this ->mysqli () ->autocommit (false );
12801288 $ this ->_transaction_in_progress = true ;
12811289 register_shutdown_function (array ($ this , "_transaction_status_check " ));
12821290 }
@@ -1288,9 +1296,9 @@ public function startTransaction () {
12881296 * @uses mysqli->autocommit(true);
12891297 */
12901298 public function commit () {
1291- $ this ->_mysqli ->commit ();
1299+ $ this ->mysqli () ->commit ();
12921300 $ this ->_transaction_in_progress = false ;
1293- $ this ->_mysqli ->autocommit (true );
1301+ $ this ->mysqli () ->autocommit (true );
12941302 }
12951303
12961304 /**
@@ -1300,9 +1308,9 @@ public function commit () {
13001308 * @uses mysqli->autocommit(true);
13011309 */
13021310 public function rollback () {
1303- $ this ->_mysqli ->rollback ();
1311+ $ this ->mysqli () ->rollback ();
13041312 $ this ->_transaction_in_progress = false ;
1305- $ this ->_mysqli ->autocommit (true );
1313+ $ this ->mysqli () ->autocommit (true );
13061314 }
13071315
13081316 /**
0 commit comments