summaryrefslogtreecommitdiff
path: root/src/Database
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-07-28 20:11:09 +0200
committermsquare <msquare@notrademark.de>2017-07-28 20:11:09 +0200
commitf82e5456d22af7e39a22a9a64e74072cf01e0a31 (patch)
treec024194bd11621e1956a659a0ec91ee7c747b40c /src/Database
parent69a1ee2bfefb43a802dea8cf0f833cbe0a00369c (diff)
dried code by introducing selectOne for select queries with only one result line expected
Diffstat (limited to 'src/Database')
-rw-r--r--src/Database/Db.php17
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