Skip to content

Commit 2bfb6ca

Browse files
committed
added rawQueryOne rawQueryValue()
1 parent fa54e4d commit 2bfb6ca

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

MysqliDb.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,49 @@ public function rawQuery ($query, $bindParams = null)
326326
return $res;
327327
}
328328

329+
/**
330+
* Helper function to execute raw SQL query and return only 1 row of results.
331+
* Note that function do not add 'limit 1' to the query by itself
332+
* Same idea as getOne()
333+
*
334+
* @param string $query User-provided query to execute.
335+
* @param array $bindParams Variables array to bind to the SQL statement.
336+
*
337+
* @return array Contains the returned row from the query.
338+
*/
339+
public function rawQueryOne ($query, $bindParams = null) {
340+
$res = $this->rawQuery ($query, $bindParams);
341+
if (is_array ($res) && isset ($res[0]))
342+
return $res[0];
343+
344+
return null;
345+
}
346+
347+
/**
348+
* Helper function to execute raw SQL query and return only 1 column of results.
349+
* If 'limit 1' will be found, then string will be returned instead of array
350+
* Same idea as getValue()
351+
*
352+
* @param string $query User-provided query to execute.
353+
* @param array $bindParams Variables array to bind to the SQL statement.
354+
*
355+
* @return mixed Contains the returned rows from the query.
356+
*/
357+
public function rawQueryValue ($query, $bindParams = null) {
358+
$res = $this->rawQuery ($query, $bindParams);
359+
if (!$res)
360+
return null;
361+
362+
$limit = preg_match ('/limit\s+1;?$/i', $query);
363+
$key = key ($res[0]);
364+
if (isset($res[0][$key]) && $limit == true)
365+
return $res[0][$key];
366+
367+
$newRes = Array ();
368+
for ($i = 0; $i < $this->count; $i++)
369+
$newRes[] = $res[$i][$key];
370+
return $newRes;
371+
}
329372
/**
330373
*
331374
* @param string $query Contains a user-provided select query.

readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,28 @@ foreach ($users as $user) {
219219
print_r ($user);
220220
}
221221
```
222+
To avoid long if checks there are couple helper functions to work with raw query select results:
223+
224+
Get 1 row of results:
225+
```php
226+
$user = $db->rawQueryOne ('select * from users where id=?', Array(10));
227+
echo $user['login'];
228+
// Object return type
229+
$user = $db->ObjectBuilder()->rawQueryOne ('select * from users where id=?', Array(10));
230+
echo $user->login;
231+
```
232+
Get 1 column value as a string:
233+
```php
234+
$password = $db->rawQueryValue ('select password from users where id=? limit 1', Array(10));
235+
echo "Password is {$password}";
236+
NOTE: for a rawQueryValue() to return string instead of an array 'limit 1' should be added to the end of the query.
237+
```
238+
Get 1 column value from multiple rows:
239+
```php
240+
$logins = $db->rawQueryValue ('select login from users limit 10');
241+
foreach ($logins as $login)
242+
echo $login;
243+
```
222244

223245
More advanced examples:
224246
```php

0 commit comments

Comments
 (0)