-
-
Save anthonyTS/89f5a831e31d970fb5e1 to your computer and use it in GitHub Desktop.
DB Class
This file contains 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 | |
class DB{ | |
private static $connection = false; | |
public static $host; | |
public static $user; | |
public static $password; | |
public static $dbname; | |
public function __construct($host, $user, $password, $dbname){ | |
self::$host = $host; | |
self::$user = $user; | |
self::$password = $password; | |
self::$dbname = $dbname; | |
if (!self::$connection) { | |
try { | |
$dsn = 'mysql:dbname=' . $dbname . ';host=' . $host; | |
self::$connection = new PDO($dsn, $user, $password); | |
} catch (PDOException $e) { | |
echo 'Connection failed: ' . $e->getMessage(); | |
} | |
} | |
} | |
public static function connect(){ | |
if (!self::$connection){ | |
try { | |
$dsn = 'mysql:dbname=' . self::$dbname . ';host=' . self::$host; | |
self::$connection = new PDO($dsn, self::$user, self::$password); | |
//$query = $dbh->query('SELECT * FROM monitoring')->fetchAll(); | |
//var_dump($query); | |
} catch (PDOException $e) { | |
echo 'Connection failed: ' . $e->getMessage(); | |
} | |
} | |
} | |
public static function showError(){ | |
$entry = self::$connection->errorInfo(); | |
print_r($entry); | |
self::log($entry); | |
} | |
public static function log($entry){ | |
$backtrace = debug_backtrace(); | |
$caller = array_pop($backtrace); | |
$caller_script = basename($caller['file']); | |
file_put_contents('/tmp/' . $caller_script . '.log', date('Y-m-d H:i:s') . "\t" . (is_array($entry) || is_object($entry) ? print_r($entry,true) : $entry) . PHP_EOL, FILE_APPEND); | |
} | |
public static function select($sql){ | |
self::connect(); | |
$result = self::$connection->query($sql); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
return $result->fetchAll(); | |
} | |
public static function single($sql){ | |
self::connect(); | |
$result = self::$connection->query($sql); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
return $result->fetch(); | |
} | |
public static function insert($sql){ | |
self::connect(); | |
$result = self::$connection->query($sql); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
return self::$connection->lastInsertId(); | |
} | |
public static function query($sql){ | |
self::connect(); | |
$result = self::$connection->query($sql); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
return true; | |
} | |
public static function getKey(){ | |
self::connect(); | |
$proc = self::$connection->prepare("SET @update_id := 0;UPDATE aws_keys SET in_use = '1', used= (used + 1), id = (SELECT @update_id := id) WHERE active = 1 AND in_use = '0' ORDER BY used ASC LIMIT 1;"); | |
$proc->execute(); | |
$proc->closeCursor(); | |
$result = self::$connection->query("SELECT * FROM aws_keys WHERE id = @update_id"); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
$key = $result->fetch(PDO::FETCH_ASSOC); | |
return $key; | |
} | |
public static function update($sql){ | |
self::connect(); | |
$stm = self::$connection->prepare($sql); | |
$result = $stm->execute(); | |
if (!$result){ | |
self::showError(); | |
return false; | |
} | |
return $stm->rowCount(); | |
} | |
public static function close(){ | |
self::$connection = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment