diff --git a/MysqliDb.php b/MysqliDb.php index ce67820b..75eb0768 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -326,6 +326,49 @@ public function rawQuery ($query, $bindParams = null) 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. @@ -441,17 +484,24 @@ public function getOne($tableName, $columns = '*') * 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 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 null; + $newRes = Array (); + for ($i = 0; $i < $this->count; $i++) + $newRes[] = $res[$i]['retval']; + return $newRes; } /** diff --git a/readme.md b/readme.md index c04b30e7..aea818ea 100644 --- a/readme.md +++ b/readme.md @@ -187,6 +187,18 @@ or select one column value or function result $count = $db->getValue ("users", "count(*)"); 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 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 @@ -207,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