Skip to content

Commit fa54e4d

Browse files
committed
Added getValue argument to fetch value from multiple rows
1 parent 2dab030 commit fa54e4d

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

MysqliDb.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -441,17 +441,24 @@ public function getOne($tableName, $columns = '*')
441441
* A convenient SELECT COLUMN function to get a single column value from one row
442442
*
443443
* @param string $tableName The name of the database table to work with.
444+
* @param int $limit Limit of rows to select. Use null for unlimited..1 by default
444445
*
445-
* @return string Contains the value of a returned column.
446+
* @return mixed Contains the value of a returned column / array of values
446447
*/
447-
public function getValue($tableName, $column)
448+
public function getValue ($tableName, $column, $limit = 1)
448449
{
449-
$res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval");
450+
$res = $this->ArrayBuilder()->get ($tableName, $limit, "{$column} AS retval");
450451

451-
if (isset($res[0]["retval"]))
452+
if (!$res)
453+
return null;
454+
455+
if (isset($res[0]["retval"]) && $limit == 1)
452456
return $res[0]["retval"];
453457

454-
return null;
458+
$newRes = Array ();
459+
for ($i = 0; $i < $this->count; $i++)
460+
$newRes[] = $res[$i]['retval'];
461+
return $newRes;
455462
}
456463

457464
/**

readme.md

Lines changed: 12 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

0 commit comments

Comments
 (0)