@@ -24,7 +24,7 @@ class MysqliDb
24
24
*
25
25
* @var string
26
26
*/
27
- public static $ prefix ;
27
+ public static $ prefix = '' ;
28
28
/**
29
29
* MySQLi instance
30
30
*
@@ -167,12 +167,8 @@ public function __construct($host = NULL, $username = NULL, $password = NULL, $d
167
167
$ this ->isSubQuery = true ;
168
168
return ;
169
169
}
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 );
176
172
self ::$ _instance = $ this ;
177
173
}
178
174
@@ -194,6 +190,17 @@ public function connect()
194
190
if ($ this ->charset )
195
191
$ this ->_mysqli ->set_charset ($ this ->charset );
196
192
}
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
+
197
204
/**
198
205
* A method of returning the static instance to allow access to the
199
206
* instantiated object from within another class.
@@ -643,7 +650,7 @@ public function groupBy($groupByField)
643
650
*/
644
651
public function getInsertId ()
645
652
{
646
- return $ this ->_mysqli ->insert_id ;
653
+ return $ this ->mysqli () ->insert_id ;
647
654
}
648
655
649
656
/**
@@ -655,7 +662,7 @@ public function getInsertId()
655
662
*/
656
663
public function escape ($ str )
657
664
{
658
- return $ this ->_mysqli ->real_escape_string ($ str );
665
+ return $ this ->mysqli () ->real_escape_string ($ str );
659
666
}
660
667
661
668
/**
@@ -667,7 +674,7 @@ public function escape($str)
667
674
* @return bool True if connection is up
668
675
*/
669
676
public function ping () {
670
- return $ this ->_mysqli ->ping ();
677
+ return $ this ->mysqli () ->ping ();
671
678
}
672
679
673
680
/**
@@ -876,11 +883,11 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
876
883
array_push ($ results , $ x );
877
884
}
878
885
// 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 ();
881
888
882
889
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() ' );
884
891
$ totalCount = $ stmt ->fetch_row ();
885
892
$ this ->totalCount = $ totalCount [0 ];
886
893
}
@@ -1074,8 +1081,8 @@ protected function _buildLimit ($numRows) {
1074
1081
*/
1075
1082
protected function _prepareQuery ()
1076
1083
{
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 );
1079
1086
}
1080
1087
if ($ this ->traceEnabled )
1081
1088
$ this ->traceStartQ = microtime (true );
@@ -1151,7 +1158,9 @@ public function getLastQuery () {
1151
1158
* @return string
1152
1159
*/
1153
1160
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 );
1155
1164
}
1156
1165
1157
1166
/**
@@ -1276,7 +1285,7 @@ public function copy ()
1276
1285
* @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
1277
1286
*/
1278
1287
public function startTransaction () {
1279
- $ this ->_mysqli ->autocommit (false );
1288
+ $ this ->mysqli () ->autocommit (false );
1280
1289
$ this ->_transaction_in_progress = true ;
1281
1290
register_shutdown_function (array ($ this , "_transaction_status_check " ));
1282
1291
}
@@ -1288,9 +1297,9 @@ public function startTransaction () {
1288
1297
* @uses mysqli->autocommit(true);
1289
1298
*/
1290
1299
public function commit () {
1291
- $ this ->_mysqli ->commit ();
1300
+ $ this ->mysqli () ->commit ();
1292
1301
$ this ->_transaction_in_progress = false ;
1293
- $ this ->_mysqli ->autocommit (true );
1302
+ $ this ->mysqli () ->autocommit (true );
1294
1303
}
1295
1304
1296
1305
/**
@@ -1300,9 +1309,9 @@ public function commit () {
1300
1309
* @uses mysqli->autocommit(true);
1301
1310
*/
1302
1311
public function rollback () {
1303
- $ this ->_mysqli ->rollback ();
1312
+ $ this ->mysqli () ->rollback ();
1304
1313
$ this ->_transaction_in_progress = false ;
1305
- $ this ->_mysqli ->autocommit (true );
1314
+ $ this ->mysqli () ->autocommit (true );
1306
1315
}
1307
1316
1308
1317
/**
0 commit comments