Skip to content

Instantly share code, notes, and snippets.

@Celleb
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Celleb/7e4a8c8b57a1787f1ab5 to your computer and use it in GitHub Desktop.
Save Celleb/7e4a8c8b57a1787f1ab5 to your computer and use it in GitHub Desktop.
This class can be used to access PDO statically and provides usefull methods to quickly access the database
<?php
/**
* Provides a way to call the PDO statically
* @author Jonas Tomanga <jonas@modus.com.na>
* @version 2.0
*/
class DB {
/**
* Holds the PDO statement object
* @var object
* @since version 2.0
*/
private static $stmt;
/**
* Creates an object to statically use pdo
* @param string $user The Database Username
* @param string $pass The Database password of the given user
* @return \PDO Returns PHP Database Object
* @static
* @since version 1.0
*/
static public function DBi($user = 'USERNAME', $pass = 'PASSWORD') {
try {
$data = 'mysql:host=host;dbname=DBNAME';
$DB = new PDO($data, $user, $pass);
return $DB;
} catch (PDOException $e) {
return self::pdoException($e);
}
}
/**
* Prepares a statement and executes the statement using the given query and parameters
* @param string $sql A string of the sql query
* @param array $params An array of the parameter
* @todo Add proper exception handling
* @since version 2.0
* @static
*/
static public function execute($sql, $params = array()) {
/* clear stmt */
self::$stmt = "";
try {
self::$stmt = self::DBi()->prepare($sql);
return self::$stmt->execute($params) ? true : false;
} catch (PDOException $e) {
return self::pdoException($e);
}
}
/**
* Fetches the results of the executed statement
* @static
* @param string $sql A string of the sql query
* @param array $params An array of the parameter
* @param int $method The code for the fetch Method to be used, defaulst to FETCH_ASSOC
* @return array Returns an array of rows from the database
* @since version 2.0
*/
static public function fetchAll($sql, $params = array(), $method = PDO::FETCH_ASSOC) {
try {
return self::execute($sql, $params) ? self::$stmt->fetchAll($method) : false;
} catch (PDOException $e) {
return self::pdoException($e);
}
}
/**
* Fetch the result of the executed statement
* @static
* @param string $sql A string of the sql query
* @param array $params An array of the parameter
* @param int $method Fetch Method
* @return array Returns an array of one from the database
* @since version 2.0
*/
static public function fetch($sql, $params = array(), $method = PDO::FETCH_ASSOC) {
try {
return self::execute($sql, $params) ? self::$stmt->fetch($method) : FALSE;
} catch (PDOException $e) {
return self::pdoException($e);
}
}
/**
* Deals with the PDO exceptions for the whole class
* @param array $e An of the PDO error detaisl
*/
static private function pdoException($e) {
$message = "Sorry a Database Error occured " . $e->getCode . " " . $e->getMessage();
$code = 0;
return array('code' => $code, 'message' => $message);
}
}
<?php
require_once 'DB.php';
$sql = "SELECT * FROM table WHERE id = :id && key = :key";
/* execute the query with a single method */
$results = DB::fetchAll($sql, array(':id' => 1, ':key' => 'BN'));
/* check if results are valid */
if(is_array($results)){
foreach($results as $results){
//process data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment