summaryrefslogtreecommitdiffstats
path: root/inc/db.inc.php
diff options
context:
space:
mode:
authorSimon Rettberg2013-10-31 12:38:25 +0100
committerSimon Rettberg2013-10-31 12:38:25 +0100
commita362ac12b119b49519f5af51b92ebb7d6e127b87 (patch)
treea2334426c8af99f864e2dd90c2f275e3ed50083a /inc/db.inc.php
parentRemodel zeug mit settings und so (diff)
downloadslx-admin-a362ac12b119b49519f5af51b92ebb7d6e127b87.tar.gz
slx-admin-a362ac12b119b49519f5af51b92ebb7d6e127b87.tar.xz
slx-admin-a362ac12b119b49519f5af51b92ebb7d6e127b87.zip
Comments, minor refactoring, possiblity to validate configuration parameters
Diffstat (limited to 'inc/db.inc.php')
-rw-r--r--inc/db.inc.php27
1 files changed, 26 insertions, 1 deletions
diff --git a/inc/db.inc.php b/inc/db.inc.php
index 09341a07..a797ae93 100644
--- a/inc/db.inc.php
+++ b/inc/db.inc.php
@@ -1,11 +1,18 @@
<?php
+/**
+ * Handle communication with the database
+ * This is a very thin layer between you and PDO.
+ */
class Database
{
private static $dbh = false;
private static $statements = array();
- public static function init()
+ /**
+ * Connect to the DB if not already connected.
+ */
+ private static function init()
{
if (self::$dbh !== false) return;
try {
@@ -15,6 +22,10 @@ class Database
}
}
+ /**
+ * If you just need the first row of a query you can use this.
+ * Will return an associative array, or false if no row matches the query
+ */
public static function queryFirst($query, $args = array())
{
$res = self::simpleQuery($query, $args);
@@ -22,6 +33,10 @@ class Database
return $res->fetch(PDO::FETCH_ASSOC);
}
+ /**
+ * Execute the given query and return the number of rows affected.
+ * Mostly useful for UPDATEs or INSERTs
+ */
public static function exec($query, $args = array())
{
$res = self::simpleQuery($query, $args);
@@ -29,6 +44,12 @@ class Database
return $res->rowCount();
}
+ /**
+ * Execute the given query and return the corresponding PDOStatement object
+ * Note that this will re-use PDOStatements, so if you run the same
+ * query again with different params, do not rely on the first PDOStatement
+ * still being valid. If you need to do something fancy, use Database::prepare
+ */
public static function simpleQuery($query, $args = array())
{
self::init();
@@ -44,6 +65,10 @@ class Database
return self::$statements[$query];
}
+ /**
+ * Simply calls PDO::prepare and returns the PDOStatement.
+ * You must call PDOStatement::execute manually on it.
+ */
public static function prepare($query)
{
self:init();