/ Published in: PHP
A simple wrapper class for MySql around PDO. Also an example on how to use this wrapper for SQLite.
Expand |
Embed | Plain Text
<?php // ------------------------------------------------------------------ // - Database class'es - // ------------------------------------------------------------------ /** * Main database class * Works as a layer over PDO. Every static call is forwarded to the PDO object, except for those which are defined * **/ class DB { protected static $DB; private function __construct() {} private function __clone() {} { try { // connects to the database self::$DB = new PDO("mysql:dbname=$dbname;host:=$host" , $user , $password); // set the error reporting attribute self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { } } { self::$DB = NULL; } { } } /** * A test class * Using SQLite with 'sqlite::memory:' so that its no need for username, password, etc * Perfect for quick testing purpose * **/ class DBtest extends DB { { try { self::$DB = new PDO("sqlite::memory:"); // connect to database self::$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the error reporting attribute } catch(PDOException $e) { } } } // ------------------------------------------------------------------ // - Initilize a standard connection - // ------------------------------------------------------------------ include 'config.php'; $dbh::connect( $config['db']['default']['dbname'], $config['db']['default']['host'], $config['db']['default']['username'], $config['db']['default']['password'] ); ?>
You need to login to post a comment.
