Commit fa54e4d6 authored by Alexander Butenko's avatar Alexander Butenko

Added getValue argument to fetch value from multiple rows

parent 2dab030a
...@@ -441,17 +441,24 @@ class MysqliDb ...@@ -441,17 +441,24 @@ class MysqliDb
* A convenient SELECT COLUMN function to get a single column value from one row * A convenient SELECT COLUMN function to get a single column value from one row
* *
* @param string $tableName The name of the database table to work with. * @param string $tableName The name of the database table to work with.
* @param int $limit Limit of rows to select. Use null for unlimited..1 by default
* *
* @return string Contains the value of a returned column. * @return mixed Contains the value of a returned column / array of values
*/ */
public function getValue($tableName, $column) public function getValue ($tableName, $column, $limit = 1)
{ {
$res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval"); $res = $this->ArrayBuilder()->get ($tableName, $limit, "{$column} AS retval");
if (isset($res[0]["retval"])) if (!$res)
return null;
if (isset($res[0]["retval"]) && $limit == 1)
return $res[0]["retval"]; return $res[0]["retval"];
return null; $newRes = Array ();
for ($i = 0; $i < $this->count; $i++)
$newRes[] = $res[$i]['retval'];
return $newRes;
} }
/** /**
......
...@@ -187,6 +187,18 @@ or select one column value or function result ...@@ -187,6 +187,18 @@ or select one column value or function result
$count = $db->getValue ("users", "count(*)"); $count = $db->getValue ("users", "count(*)");
echo "{$count} users found"; echo "{$count} users found";
``` ```
select one column value or function result from multiple rows:
``php
$logins = $db->getValue ("users", "login", null);
// select login from users
$logins = $db->getValue ("users", "login", 5);
// select login from users limit 5
foreach ($logins as $login)
echo $login;
```
### Defining a return type ### Defining a return type
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 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
```php ```php
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment