Skip to content

Instantly share code, notes, and snippets.

@davidbuchweitz-zz
Last active October 8, 2019 19:52
Show Gist options
  • Select an option

  • Save davidbuchweitz-zz/8c2b897c4d9e331cd916fd6fd9aec5f5 to your computer and use it in GitHub Desktop.

Select an option

Save davidbuchweitz-zz/8c2b897c4d9e331cd916fd6fd9aec5f5 to your computer and use it in GitHub Desktop.
Micro PDO Wrapper
<?php
/**
* µPDO - Micro PDO wrapper
*
* A tiny wrapper for trusted queries.
*
* https://gist.github.com/davidbuchweitz/8c2b897c4d9e331cd916fd6fd9aec5f5
*/
class uPDO
{
public static $host = "127.0.0.1";
public static $user = "root";
public static $pass = "";
private static $_dbh = null;
public static function __callStatic($func, $args)
{
if (!static::$_dbh)
static::$_dbh = new PDO("mysql:host=".static::$host, static::$user, static::$pass);
static::$_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = static::$_dbh->prepare($args[0]);
$result = $sth->execute();
switch($func)
{
case "all" : return $sth->fetchAll(PDO::FETCH_ASSOC);
case "single" : return $sth->fetch(PDO::FETCH_ASSOC);
case "scalar" : return $sth->fetchColumn();
case "insert" : return static::$_dbh->lastInsertId();
case "exec" : return ($result !== false) ? $sth->rowCount() : false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment