Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
bbd80bc5
Commit
bbd80bc5
authored
Apr 20, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
withTotalCount() method to return total number of rows
parent
32b81aa1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
4 deletions
+42
-4
MysqliDb.php
MysqliDb.php
+28
-2
readme.md
readme.md
+8
-1
tests.php
tests.php
+6
-1
No files found.
MysqliDb.php
View file @
bbd80bc5
...
...
@@ -64,7 +64,7 @@ class MysqliDb
*/
protected
$_groupBy
=
array
();
/**
* Dynamic array that holds a combination of where condition/table data value types and parameter refer
a
nces
* Dynamic array that holds a combination of where condition/table data value types and parameter refer
e
nces
*
* @var array
*/
...
...
@@ -75,6 +75,13 @@ class MysqliDb
* @var string
*/
public
$count
=
0
;
/**
* Variable which holds an amount of returned rows during get/getOne/select queries with withTotalCount()
*
* @var string
*/
public
$totalCount
=
0
;
protected
$fetchTotalCount
=
false
;
/**
* Variable which holds last statement error
*
...
...
@@ -253,6 +260,16 @@ class MysqliDb
return
$this
->
_dynamicBindResults
(
$stmt
);
}
/**
* Function to enable SQL_CALC_FOUND_ROWS in the get queries
*
* @return MysqliDb
*/
public
function
withTotalCount
()
{
$this
->
fetchTotalCount
=
true
;
return
$this
;
}
/**
* A convenient SELECT * function.
*
...
...
@@ -266,8 +283,9 @@ class MysqliDb
if
(
empty
(
$columns
))
$columns
=
'*'
;
$this
->
_query
=
$this
->
fetchTotalCount
==
true
?
'SELECT SQL_CALC_FOUND_ROWS '
:
'SELECT '
;
$column
=
is_array
(
$columns
)
?
implode
(
', '
,
$columns
)
:
$columns
;
$this
->
_query
=
"SELECT
$column
FROM "
.
self
::
$_prefix
.
$tableName
;
$this
->
_query
.=
"
$column
FROM "
.
self
::
$_prefix
.
$tableName
;
$stmt
=
$this
->
_buildQuery
(
$numRows
);
if
(
$this
->
isSubQuery
)
...
...
@@ -677,6 +695,7 @@ class MysqliDb
call_user_func_array
(
array
(
$stmt
,
'bind_result'
),
$parameters
);
$this
->
totalCount
=
0
;
$this
->
count
=
0
;
while
(
$stmt
->
fetch
())
{
$x
=
array
();
...
...
@@ -687,6 +706,13 @@ class MysqliDb
array_push
(
$results
,
$x
);
}
if
(
$this
->
fetchTotalCount
===
true
)
{
$this
->
fetchTotalCount
=
false
;
$stmt
=
$this
->
_mysqli
->
query
(
'SELECT FOUND_ROWS();'
);
$totalCount
=
$stmt
->
fetch_row
();
$this
->
totalCount
=
$totalCount
[
0
];
}
return
$results
;
}
...
...
readme.md
View file @
bbd80bc5
...
...
@@ -265,6 +265,14 @@ $res = $db->get ("users");
```
Find the total number of rows matched. Simple pagination example:
```
php
$offset
=
10
;
$count
=
15
;
$users
=
$db
->
withTotalCount
()
->
get
(
'users'
,
Array
(
$offset
,
$count
));
echo
"Showing
{
$count
}
from
{
$db
->
totalCount
}
"
;
```
Optionally you can use method chaining to call where multiple times without referencing your object over an over:
```
php
...
...
@@ -309,7 +317,6 @@ print_r ($products);
### Properties sharing
Its is also possible to copy properties
Simple pagination example:
```
php
$db
->
where
(
"agentId"
,
10
);
$db
->
where
(
"active"
,
true
);
...
...
tests.php
View file @
bbd80bc5
...
...
@@ -163,7 +163,6 @@ if ($db->count != 3) {
echo
"Invalid total insert count"
;
exit
;
}
echo
$db
->
getLastQuery
();
// order by field
$db
->
orderBy
(
"login"
,
"asc"
,
Array
(
"user3"
,
"user2"
,
"user1"
));
...
...
@@ -341,6 +340,12 @@ if ($db->count != 5) {
echo
"invalid join with subquery count"
;
exit
;
}
$db
->
withTotalCount
()
->
get
(
'users'
,
1
);
if
(
$db
->
totalCount
!=
3
)
{
echo
"error in totalCount"
;
exit
;
}
///
//TODO: insert test
$db
->
delete
(
"users"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment