@@ -43,6 +43,12 @@ class MysqliDb
4343 * @var string
4444 */
4545 protected $ _lastQuery ;
46+ /**
47+ * The SQL query options required after SELECT, INSERT, UPDATE or DELETE
48+ *
49+ * @var string
50+ */
51+ protected $ _queryOptions = array ();
4652 /**
4753 * An array that holds where joins
4854 *
@@ -81,7 +87,6 @@ class MysqliDb
8187 * @var string
8288 */
8389 public $ totalCount = 0 ;
84- protected $ fetchTotalCount = false ;
8590 /**
8691 * Variable which holds last statement error
8792 *
@@ -193,7 +198,7 @@ protected function reset()
193198 $ this ->_groupBy = array ();
194199 $ this ->_bindParams = array ('' ); // Create the empty 0 index
195200 $ this ->_query = null ;
196- $ this ->count = 0 ;
201+ $ this ->_queryOptions = array () ;
197202 }
198203
199204 /**
@@ -238,9 +243,10 @@ public function rawQuery ($query, $bindParams = null, $sanitize = true)
238243 $ stmt ->execute ();
239244 $ this ->_stmtError = $ stmt ->error ;
240245 $ this ->_lastQuery = $ this ->replacePlaceHolders ($ this ->_query , $ params );
246+ $ res = $ this ->_dynamicBindResults ($ stmt );
241247 $ this ->reset ();
242248
243- return $ this -> _dynamicBindResults ( $ stmt ) ;
249+ return $ res ;
244250 }
245251
246252 /**
@@ -256,9 +262,28 @@ public function query($query, $numRows = null)
256262 $ stmt = $ this ->_buildQuery ($ numRows );
257263 $ stmt ->execute ();
258264 $ this ->_stmtError = $ stmt ->error ;
265+ $ res = $ this ->_dynamicBindResults ($ stmt );
259266 $ this ->reset ();
260267
261- return $ this ->_dynamicBindResults ($ stmt );
268+ return $ res ;
269+ }
270+
271+ /**
272+ * This method allows you to specify multiple (method chaining optional) options for SQL queries.
273+ *
274+ * @uses $MySqliDb->setQueryOption('name');
275+ *
276+ * @param string/array $options The optons name of the query.
277+ *
278+ * @return MysqliDb
279+ */
280+ public function setQueryOption ($ options ) {
281+ if (is_array ($ options ))
282+ $ this ->_queryOptions = array_merge ($ this ->_queryOptions , $ options );
283+ else
284+ $ this ->_queryOptions [] = $ options ;
285+
286+ return $ this ;
262287 }
263288
264289 /**
@@ -267,7 +292,7 @@ public function query($query, $numRows = null)
267292 * @return MysqliDb
268293 */
269294 public function withTotalCount () {
270- $ this ->fetchTotalCount = true ;
295+ $ this ->setQueryOption ( ' SQL_CALC_FOUND_ROWS ' ) ;
271296 return $ this ;
272297 }
273298
@@ -284,19 +309,20 @@ public function get($tableName, $numRows = null, $columns = '*')
284309 if (empty ($ columns ))
285310 $ columns = '* ' ;
286311
287- $ this ->_query = $ this ->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ' ;
288312 $ column = is_array ($ columns ) ? implode (', ' , $ columns ) : $ columns ;
289- $ this ->_query .= "$ column FROM " .self ::$ _prefix . $ tableName ;
313+ $ this ->_query = 'SELECT ' . implode (' ' , $ this ->_queryOptions ) . ' ' .
314+ $ column . " FROM " .self ::$ _prefix . $ tableName ;
290315 $ stmt = $ this ->_buildQuery ($ numRows );
291316
292317 if ($ this ->isSubQuery )
293318 return $ this ;
294319
295320 $ stmt ->execute ();
296321 $ this ->_stmtError = $ stmt ->error ;
322+ $ res = $ this ->_dynamicBindResults ($ stmt );
297323 $ this ->reset ();
298324
299- return $ this -> _dynamicBindResults ( $ stmt ) ;
325+ return $ res ;
300326 }
301327
302328 /**
@@ -724,9 +750,8 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
724750 if ($ this ->_mysqli ->more_results ())
725751 $ this ->_mysqli ->next_result ();
726752
727- if ($ this ->fetchTotalCount === true ) {
728- $ this ->fetchTotalCount = false ;
729- $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS(); ' );
753+ if (in_array ('SQL_CALC_FOUND_ROWS ' , $ this ->_queryOptions )) {
754+ $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS() ' );
730755 $ totalCount = $ stmt ->fetch_row ();
731756 $ this ->totalCount = $ totalCount [0 ];
732757 }
0 commit comments