Last active
February 13, 2024 11:00
-
-
Save elifiner/bb6a18a28a7c985095618126520e3460 to your computer and use it in GitHub Desktop.
A simple database interface for PHP
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 | |
/** | |
* A simple wrapper around PDO for building quick DB accessing scripts. | |
* Was built during the Jun 19th, 2017 data recovery fiasco at RetreatGuru. | |
*/ | |
class Database { | |
public $db; | |
public function __construct($engine, $host, $database, $username, $password) { | |
$this->db = new PDO("$engine:host=$host;dbname=$database", $username, $password); | |
} | |
public function query($sql) { | |
$result = $this->db->query($sql); | |
if ($result == false) { | |
throw new Exception($db->errorInfo()[1]); | |
} | |
return $result; | |
} | |
public function get_rows($sql) { | |
return $this->query($sql)->fetchAll(); | |
} | |
public function get_col($sql) { | |
return array_column($this->get_rows($sql), 0); | |
} | |
} | |
$db = new Database('mysql', '127.0.0.1', 'prod', 'root', 'root'); | |
var_export( | |
$db->get_col('select table_name from information_schema.tables') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment