Skip to content

Commit 031d510

Browse files
committed
Merge pull request #292 from avbdr/master
fixes
2 parents fcc0204 + 2bfb6ca commit 031d510

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

MysqliDb.php

Lines changed: 55 additions & 5 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.
@@ -441,17 +484,24 @@ public function getOne($tableName, $columns = '*')
441484
* A convenient SELECT COLUMN function to get a single column value from one row
442485
*
443486
* @param string $tableName The name of the database table to work with.
487+
* @param int $limit Limit of rows to select. Use null for unlimited..1 by default
444488
*
445-
* @return string Contains the value of a returned column.
489+
* @return mixed Contains the value of a returned column / array of values
446490
*/
447-
public function getValue($tableName, $column)
491+
public function getValue ($tableName, $column, $limit = 1)
448492
{
449-
$res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval");
493+
$res = $this->ArrayBuilder()->get ($tableName, $limit, "{$column} AS retval");
450494

451-
if (isset($res[0]["retval"]))
495+
if (!$res)
496+
return null;
497+
498+
if (isset($res[0]["retval"]) && $limit == 1)
452499
return $res[0]["retval"];
453500

454-
return null;
501+
$newRes = Array ();
502+
for ($i = 0; $i < $this->count; $i++)
503+
$newRes[] = $res[$i]['retval'];
504+
return $newRes;
455505
}
456506

457507
/**

readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,18 @@ or select one column value or function result
187187
$count = $db->getValue ("users", "count(*)");
188188
echo "{$count} users found";
189189
```
190+
191+
select one column value or function result from multiple rows:
192+
``php
193+
$logins = $db->getValue ("users", "login", null);
194+
// select login from users
195+
$logins = $db->getValue ("users", "login", 5);
196+
// select login from users limit 5
197+
foreach ($logins as $login)
198+
echo $login;
199+
```
200+
201+
190202
### Defining a return type
191203
MysqliDb can return result in 3 different formats: Array of Array, Array of Objects and a Json string. To select a return type use ArrayBuilder(), ObjectBuilder() and JsonBuilder() methods. Note that ArrayBuilder() is a default return type
192204
```php
@@ -207,6 +219,28 @@ foreach ($users as $user) {
207219
print_r ($user);
208220
}
209221
```
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+
```
210244

211245
More advanced examples:
212246
```php

0 commit comments

Comments
 (0)