diff options
author | msquare <msquare@notrademark.de> | 2017-07-28 20:11:09 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2017-07-28 20:11:09 +0200 |
commit | f82e5456d22af7e39a22a9a64e74072cf01e0a31 (patch) | |
tree | c024194bd11621e1956a659a0ec91ee7c747b40c /src | |
parent | 69a1ee2bfefb43a802dea8cf0f833cbe0a00369c (diff) |
dried code by introducing selectOne for select queries with only one result line expected
Diffstat (limited to 'src')
-rw-r--r-- | src/Database/Db.php | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/Database/Db.php b/src/Database/Db.php index b433f40d..9422ae77 100644 --- a/src/Database/Db.php +++ b/src/Database/Db.php @@ -79,6 +79,23 @@ class Db return self::$stm->fetchAll(PDO::FETCH_ASSOC); } + + /** + * Run a select query and return only the first result or null if no result is found. + * @param string $query + * @param array $bindings + * @return array|null + */ + public static function selectOne($query, array $bindings = []) + { + $result = self::select($query); + + if(empty($result)) { + return null; + } + + return array_shift($result); + } /** * Run an insert query |