Skip to content

Instantly share code, notes, and snippets.

@jasonsilberman
Created June 26, 2014 16:36
Show Gist options
  • Save jasonsilberman/9f6471220ef187343f54 to your computer and use it in GitHub Desktop.
Save jasonsilberman/9f6471220ef187343f54 to your computer and use it in GitHub Desktop.
DB class
class DB {
protected static $dbn;
public static function connect() {
$p = Config::valueForKeyPath('db');
static::$dbn = new PDO('mysql:host=' . $p['host'] . ';dbname=' . $p['database'], $p['user'], $p['pass']);
static::$dbn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return true;
}
public static function close() {
static::$dbn = null;
}
public static function quote($str) {
return static::$dbn->quote($str);
}
public static function query($sql, $binds = array()) {
if (is_null(static::$dbn)) static::connect();
$binds = (array) $binds;
$pr = static::$dbn->prepare($sql);
Console::log('DB Query:', $pr->queryString);
$pr->execute($binds);
return $pr;
}
public static function exec($sql, $binds = array()) {
if (is_null(static::$dbn)) static::connect();
$binds = (array) $binds;
$pr = static::$dbn->prepare($sql);
Console::log('DB Query:', $pr->queryString);
$exData = $pr->execute($binds);
return $exData;
}
public static function row($sql, $binds = array(), $style = PDO::FETCH_OBJ) {
return static::query($sql, $binds)->fetch($style);
}
public static function rows($sql, $binds = array(), $style = PDO::FETCH_OBJ) {
return static::query($sql, $binds)->fetchAll($style);
}
public static function insert_id() {
return static::$dbn->lastInsertId();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment