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
2bfb6ca2
Commit
2bfb6ca2
authored
Aug 12, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added rawQueryOne rawQueryValue()
parent
fa54e4d6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
0 deletions
+65
-0
MysqliDb.php
MysqliDb.php
+43
-0
readme.md
readme.md
+22
-0
No files found.
MysqliDb.php
View file @
2bfb6ca2
...
...
@@ -326,6 +326,49 @@ class MysqliDb
return
$res
;
}
/**
* Helper function to execute raw SQL query and return only 1 row of results.
* Note that function do not add 'limit 1' to the query by itself
* Same idea as getOne()
*
* @param string $query User-provided query to execute.
* @param array $bindParams Variables array to bind to the SQL statement.
*
* @return array Contains the returned row from the query.
*/
public
function
rawQueryOne
(
$query
,
$bindParams
=
null
)
{
$res
=
$this
->
rawQuery
(
$query
,
$bindParams
);
if
(
is_array
(
$res
)
&&
isset
(
$res
[
0
]))
return
$res
[
0
];
return
null
;
}
/**
* Helper function to execute raw SQL query and return only 1 column of results.
* If 'limit 1' will be found, then string will be returned instead of array
* Same idea as getValue()
*
* @param string $query User-provided query to execute.
* @param array $bindParams Variables array to bind to the SQL statement.
*
* @return mixed Contains the returned rows from the query.
*/
public
function
rawQueryValue
(
$query
,
$bindParams
=
null
)
{
$res
=
$this
->
rawQuery
(
$query
,
$bindParams
);
if
(
!
$res
)
return
null
;
$limit
=
preg_match
(
'/limit\s+1;?$/i'
,
$query
);
$key
=
key
(
$res
[
0
]);
if
(
isset
(
$res
[
0
][
$key
])
&&
$limit
==
true
)
return
$res
[
0
][
$key
];
$newRes
=
Array
();
for
(
$i
=
0
;
$i
<
$this
->
count
;
$i
++
)
$newRes
[]
=
$res
[
$i
][
$key
];
return
$newRes
;
}
/**
*
* @param string $query Contains a user-provided select query.
...
...
readme.md
View file @
2bfb6ca2
...
...
@@ -219,6 +219,28 @@ foreach ($users as $user) {
print_r ($user);
}
```
To avoid long if checks there are couple helper functions to work with raw query select results:
Get 1 row of results:
```php
$user = $db->rawQueryOne ('select * from users where id=?', Array(10));
echo $user['login'];
// Object return type
$user = $db->ObjectBuilder()->rawQueryOne ('select * from users where id=?', Array(10));
echo $user->login;
```
Get 1 column value as a string:
```php
$password = $db->rawQueryValue ('select password from users where id=? limit 1', Array(10));
echo "Password is {$password}";
NOTE: for a rawQueryValue() to return string instead of an array 'limit 1' should be added to the end of the query.
```
Get 1 column value from multiple rows:
```php
$logins = $db->rawQueryValue ('select login from users limit 10');
foreach ($logins as $login)
echo $login;
```
More advanced examples:
```php
...
...
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