@@ -24,7 +24,7 @@ class MysqliDb
2424 *
2525 * @var string
2626 */
27- public static $ prefix ;
27+ public static $ prefix = '' ;
2828 /**
2929 * MySQLi instance
3030 *
@@ -167,12 +167,8 @@ 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-
175- $ this ->setPrefix ();
170+ if (isset ($ prefix ))
171+ $ this ->setPrefix ($ prefix );
176172 self ::$ _instance = $ this ;
177173 }
178174
@@ -194,6 +190,17 @@ public function connect()
194190 if ($ this ->charset )
195191 $ this ->_mysqli ->set_charset ($ this ->charset );
196192 }
193+
194+ /**
195+ * A method to get mysqli object or create it in case needed
196+ */
197+ public function mysqli ()
198+ {
199+ if (!$ this ->_mysqli )
200+ $ this ->connect ();
201+ return $ this ->_mysqli ;
202+ }
203+
197204 /**
198205 * A method of returning the static instance to allow access to the
199206 * instantiated object from within another class.
@@ -643,7 +650,7 @@ public function groupBy($groupByField)
643650 */
644651 public function getInsertId ()
645652 {
646- return $ this ->_mysqli ->insert_id ;
653+ return $ this ->mysqli () ->insert_id ;
647654 }
648655
649656 /**
@@ -655,7 +662,7 @@ public function getInsertId()
655662 */
656663 public function escape ($ str )
657664 {
658- return $ this ->_mysqli ->real_escape_string ($ str );
665+ return $ this ->mysqli () ->real_escape_string ($ str );
659666 }
660667
661668 /**
@@ -667,7 +674,7 @@ public function escape($str)
667674 * @return bool True if connection is up
668675 */
669676 public function ping () {
670- return $ this ->_mysqli ->ping ();
677+ return $ this ->mysqli () ->ping ();
671678 }
672679
673680 /**
@@ -876,11 +883,11 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
876883 array_push ($ results , $ x );
877884 }
878885 // stored procedures sometimes can return more then 1 resultset
879- if ($ this ->_mysqli ->more_results ())
880- $ this ->_mysqli ->next_result ();
886+ if ($ this ->mysqli () ->more_results ())
887+ $ this ->mysqli () ->next_result ();
881888
882889 if (in_array ('SQL_CALC_FOUND_ROWS ' , $ this ->_queryOptions )) {
883- $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS() ' );
890+ $ stmt = $ this ->mysqli () ->query ('SELECT FOUND_ROWS() ' );
884891 $ totalCount = $ stmt ->fetch_row ();
885892 $ this ->totalCount = $ totalCount [0 ];
886893 }
@@ -1074,8 +1081,8 @@ protected function _buildLimit ($numRows) {
10741081 */
10751082 protected function _prepareQuery ()
10761083 {
1077- if (!$ stmt = $ this ->_mysqli ->prepare ($ this ->_query )) {
1078- trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->_mysqli ->error , E_USER_ERROR );
1084+ if (!$ stmt = $ this ->mysqli () ->prepare ($ this ->_query )) {
1085+ trigger_error ("Problem preparing query ( $ this ->_query ) " . $ this ->mysqli () ->error , E_USER_ERROR );
10791086 }
10801087 if ($ this ->traceEnabled )
10811088 $ this ->traceStartQ = microtime (true );
@@ -1151,7 +1158,9 @@ public function getLastQuery () {
11511158 * @return string
11521159 */
11531160 public function getLastError () {
1154- return trim ($ this ->_stmtError . " " . $ this ->_mysqli ->error );
1161+ if (!$ this ->_mysqli )
1162+ return "mysqli is null " ;
1163+ return trim ($ this ->_stmtError . " " . $ this ->mysqli ()->error );
11551164 }
11561165
11571166 /**
@@ -1276,7 +1285,7 @@ public function copy ()
12761285 * @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
12771286 */
12781287 public function startTransaction () {
1279- $ this ->_mysqli ->autocommit (false );
1288+ $ this ->mysqli () ->autocommit (false );
12801289 $ this ->_transaction_in_progress = true ;
12811290 register_shutdown_function (array ($ this , "_transaction_status_check " ));
12821291 }
@@ -1288,9 +1297,9 @@ public function startTransaction () {
12881297 * @uses mysqli->autocommit(true);
12891298 */
12901299 public function commit () {
1291- $ this ->_mysqli ->commit ();
1300+ $ this ->mysqli () ->commit ();
12921301 $ this ->_transaction_in_progress = false ;
1293- $ this ->_mysqli ->autocommit (true );
1302+ $ this ->mysqli () ->autocommit (true );
12941303 }
12951304
12961305 /**
@@ -1300,9 +1309,9 @@ public function commit () {
13001309 * @uses mysqli->autocommit(true);
13011310 */
13021311 public function rollback () {
1303- $ this ->_mysqli ->rollback ();
1312+ $ this ->mysqli () ->rollback ();
13041313 $ this ->_transaction_in_progress = false ;
1305- $ this ->_mysqli ->autocommit (true );
1314+ $ this ->mysqli () ->autocommit (true );
13061315 }
13071316
13081317 /**
0 commit comments