diff options
author | canyonknight <canyonknight@gmail.com> | 2013-02-03 16:26:28 +0000 |
---|---|---|
committer | Lukas Fleischer <archlinux@cryptocrack.de> | 2013-02-10 12:10:37 +0100 |
commit | 8e03e68d687015b5cd8c9d3857e1a1d007252afa (patch) | |
tree | 26cd5d7aa6cfac4f6fddfa1f76fdc59d2cbd4a0f /web/lib/DB.class.php | |
parent | b3a2b6c4a59a2cd90b020120b64f16797af1b6fb (diff) |
Add database wrapper class and new connection method
Uses the Singleton pattern to ensure all queries use the same
database connection that is released upon script completion.
All database connections should now be called with DB::connect() and
not db_connect().
Signed-off-by: canyonknight <canyonknight@gmail.com>
Signed-off-by: Lukas Fleischer <archlinux@cryptocrack.de>
Diffstat (limited to 'web/lib/DB.class.php')
-rw-r--r-- | web/lib/DB.class.php | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/web/lib/DB.class.php b/web/lib/DB.class.php new file mode 100644 index 0000000..0975989 --- /dev/null +++ b/web/lib/DB.class.php @@ -0,0 +1,28 @@ +<?php + +class DB { + + /** + * A database object + */ + private static $dbh = null; + + /** + * Return an already existing database object or newly instantiated object + * + * @return \PDO A database connection using PDO + */ + public static function connect() { + if (self::$dbh === null) { + try { + self::$dbh = new PDO(AUR_db_DSN_prefix . ":" . AUR_db_host + . ";dbname=" . AUR_db_name, AUR_db_user, AUR_db_pass); + self::$dbh->exec("SET NAMES 'utf8' COLLATE 'utf8_general_ci';"); + } catch (PDOException $e) { + die('Error - Could not connect to AUR database'); + } + } + + return self::$dbh; + } +} |