Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB
- {
- protected $dialect = 'mysql';
- protected $host = 'localhost';
- protected $db;
- protected $user;
- protected $password;
- public $conn;
- function __construct($user, $pass, $db, $host='localhost', $dialect='mysql')
- {
- $this->db = $db;
- $this->user = $user;
- $this->password = $pass;
- $this->host = $host;
- $this->dialect = $dialect;
- $this->connect();
- }
- protected function connect()
- {
- try
- {
- $this->conn = new PDO(
- $this->get_db_string(),
- $this->user,
- $this->password
- );
- }
- catch(PDOException $e)
- {
- die("<h1>Error Establishing database connections</h1>");
- }
- }
- protected function get_db_string()
- {
- return sprintf(
- '%s:host=%s;dbname=%s',
- $this->dialect,
- $this->host,
- $this->db
- );
- }
- public function get_pk($table, $pk)
- {
- $rv = array();
- if($stm = $this->conn->prepare("SELECT * FROM {$table} WHERE id = :pk"))
- {
- $stm->execute(array('pk' => $pk));
- $rv = $stm->fetchAll(PDO::FETCH_ASSOC);
- $stm->closeCursor();
- }
- return count($rv) >= 1 ? $rv[0] : false;
- }
- }
Add Comment
Please, Sign In to add comment