@@ -64,7 +64,7 @@ class MysqliDb
6464 */
6565 protected $ _groupBy = array ();
6666 /**
67- * Dynamic array that holds a combination of where condition/table data value types and parameter referances
67+ * Dynamic array that holds a combination of where condition/table data value types and parameter references
6868 *
6969 * @var array
7070 */
@@ -75,6 +75,13 @@ class MysqliDb
7575 * @var string
7676 */
7777 public $ count = 0 ;
78+ /**
79+ * Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
80+ *
81+ * @var string
82+ */
83+ public $ totalCount = 0 ;
84+ protected $ fetchTotalCount = false ;
7885 /**
7986 * Variable which holds last statement error
8087 *
@@ -253,6 +260,16 @@ public function query($query, $numRows = null)
253260 return $ this ->_dynamicBindResults ($ stmt );
254261 }
255262
263+ /**
264+ * Function to enable SQL_CALC_FOUND_ROWS in the get queries
265+ *
266+ * @return MysqliDb
267+ */
268+ public function withTotalCount () {
269+ $ this ->fetchTotalCount = true ;
270+ return $ this ;
271+ }
272+
256273 /**
257274 * A convenient SELECT * function.
258275 *
@@ -266,8 +283,9 @@ public function get($tableName, $numRows = null, $columns = '*')
266283 if (empty ($ columns ))
267284 $ columns = '* ' ;
268285
286+ $ this ->_query = $ this ->fetchTotalCount == true ? 'SELECT SQL_CALC_FOUND_ROWS ' : 'SELECT ' ;
269287 $ column = is_array ($ columns ) ? implode (', ' , $ columns ) : $ columns ;
270- $ this ->_query = "SELECT $ column FROM " .self ::$ _prefix . $ tableName ;
288+ $ this ->_query . = "$ column FROM " .self ::$ _prefix . $ tableName ;
271289 $ stmt = $ this ->_buildQuery ($ numRows );
272290
273291 if ($ this ->isSubQuery )
@@ -677,6 +695,7 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
677695
678696 call_user_func_array (array ($ stmt , 'bind_result ' ), $ parameters );
679697
698+ $ this ->totalCount = 0 ;
680699 $ this ->count = 0 ;
681700 while ($ stmt ->fetch ()) {
682701 $ x = array ();
@@ -687,6 +706,13 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
687706 array_push ($ results , $ x );
688707 }
689708
709+ if ($ this ->fetchTotalCount === true ) {
710+ $ this ->fetchTotalCount = false ;
711+ $ stmt = $ this ->_mysqli ->query ('SELECT FOUND_ROWS(); ' );
712+ $ totalCount = $ stmt ->fetch_row ();
713+ $ this ->totalCount = $ totalCount [0 ];
714+ }
715+
690716 return $ results ;
691717 }
692718
0 commit comments