Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. <?php
  2.  
  3. class Database
  4. {
  5. private $host = 'localhost';
  6. private $user = 'root';
  7. private $pass = '';
  8. private $dbname = 'myblog';
  9.  
  10. private $dbh;
  11. private $error;
  12. private $stmt;
  13.  
  14. public function __construct()
  15. {
  16. //Set DSN
  17. $dns = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
  18. //Set Options
  19. $options = array(
  20. PDO::ATTR_PERSISTENT => true,
  21. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  22. );
  23.  
  24. //Create new PDO
  25. try {
  26. $this->dbh = new PDO($dns, $this->user, $this->pass, $options);
  27. } catch (PDOException $e) {
  28.  
  29. $this->error = $e->getMessage();
  30. }
  31.  
  32. }
  33.  
  34. public function query($query)
  35. {
  36. $this->stmt = $this->dbh->prepare($query);
  37.  
  38. }
  39.  
  40. public function bind($param, $value, $type = null)
  41. {
  42. if (is_null($type)) {
  43. switch (true) {
  44. case is_int($value):
  45. $type = PDO::PARAM_INT;
  46. break;
  47. case is_bool($value):
  48. $type = PDO::PARAM_BOOL;
  49. break;
  50. case is_null($value):
  51. $type = PDO::PARAM_NULL;
  52. break;
  53. default:
  54. $type = PDO::PARAM_STR;
  55. }
  56. }
  57. $this->stmt->binValue($param, $value, $type);
  58. }
  59.  
  60. public function execute()
  61. {
  62. $this->stmt->execute();
  63. }
  64.  
  65. public function resultset()
  66. {
  67. $this->execute();
  68. return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement