mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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>
28 lines
607 B
PHP
28 lines
607 B
PHP
<?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;
|
|
}
|
|
}
|