Skip to content

Commit bbd80bc

Browse files
committed
withTotalCount() method to return total number of rows
1 parent 32b81aa commit bbd80bc

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

MysqliDb.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

readme.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,14 @@ $res = $db->get ("users");
265265
```
266266

267267

268+
Find the total number of rows matched. Simple pagination example:
269+
```php
270+
$offset = 10;
271+
$count = 15;
272+
$users = $db->withTotalCount()->get('users', Array ($offset, $count));
273+
echo "Showing {$count} from {$db->totalCount}";
274+
```
275+
268276
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
269277

270278
```php
@@ -309,7 +317,6 @@ print_r ($products);
309317
### Properties sharing
310318
Its is also possible to copy properties
311319

312-
Simple pagination example:
313320
```php
314321
$db->where ("agentId", 10);
315322
$db->where ("active", true);

tests.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ function createTable ($name, $data) {
163163
echo "Invalid total insert count";
164164
exit;
165165
}
166-
echo $db->getLastQuery();
167166

168167
// order by field
169168
$db->orderBy("login","asc", Array ("user3","user2","user1"));
@@ -341,6 +340,12 @@ function createTable ($name, $data) {
341340
echo "invalid join with subquery count";
342341
exit;
343342
}
343+
344+
$db->withTotalCount()->get('users', 1);
345+
if ($db->totalCount != 3) {
346+
echo "error in totalCount";
347+
exit;
348+
}
344349
///
345350
//TODO: insert test
346351
$db->delete("users");

0 commit comments

Comments
 (0)