Guest User

Untitled

a guest
Sep 21st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. <?php
  2. class DB
  3. {
  4. protected $dialect = 'mysql';
  5. protected $host = 'localhost';
  6. protected $db;
  7. protected $user;
  8. protected $password;
  9. public $conn;
  10.  
  11. function __construct($user, $pass, $db, $host='localhost', $dialect='mysql')
  12. {
  13. $this->db = $db;
  14. $this->user = $user;
  15. $this->password = $pass;
  16. $this->host = $host;
  17. $this->dialect = $dialect;
  18. $this->connect();
  19. }
  20.  
  21. protected function connect()
  22. {
  23. try
  24. {
  25. $this->conn = new PDO(
  26. $this->get_db_string(),
  27. $this->user,
  28. $this->password
  29. );
  30. }
  31. catch(PDOException $e)
  32. {
  33. die("<h1>Error Establishing database connections</h1>");
  34. }
  35. }
  36.  
  37. protected function get_db_string()
  38. {
  39. return sprintf(
  40. '%s:host=%s;dbname=%s',
  41. $this->dialect,
  42. $this->host,
  43. $this->db
  44. );
  45. }
  46.  
  47. public function get_pk($table, $pk)
  48. {
  49. $rv = array();
  50. if($stm = $this->conn->prepare("SELECT * FROM {$table} WHERE id = :pk"))
  51. {
  52. $stm->execute(array('pk' => $pk));
  53. $rv = $stm->fetchAll(PDO::FETCH_ASSOC);
  54. $stm->closeCursor();
  55. }
  56. return count($rv) >= 1 ? $rv[0] : false;
  57. }
  58. }
Add Comment
Please, Sign In to add comment