Last active
October 8, 2019 19:52
-
-
Save davidbuchweitz-zz/8c2b897c4d9e331cd916fd6fd9aec5f5 to your computer and use it in GitHub Desktop.
Micro PDO Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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