Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. require 'databasesclass.php';
  4.  
  5. $database = new Database();
  6.  
  7. $database->query('SELECT * FROM users');
  8. $rows = $database->resultset();
  9.  
  10. print_r($rows);
  11.  
  12. <?php
  13.  
  14.  
  15. class Database{
  16. private $host = 'localhost:8889';
  17. private $user = 'root';
  18. private $pass = 'root';
  19. private $dbname = 'register';
  20.  
  21. private $dbh;
  22. private $error;
  23. private $stmt;
  24.  
  25. public function __construct(){
  26. // Set the DSN
  27. $dsn = 'mysql:host='. $this->host . ';dbname=' . $this->dbname . $this->user . $this->pass;
  28. // set options
  29. $options = array(
  30.  
  31. PDO::ATTR_PERSISTENT => true,
  32. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION );
  33.  
  34. try {
  35. $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  36. } catch (PDOException $e) {
  37. $this->error = $e->getMessage();
  38. }
  39. }
  40. public function query($query){
  41.  
  42. $this->stmt = $this->dbh->prepare($query);
  43.  
  44. }
  45.  
  46.  
  47. public function bind($pram, $value, $type = null){
  48.  
  49. if (is_null($type)){
  50. switch(true) {
  51. case is_int($value):
  52. $type = PDO::PRAM_INT;
  53. break;
  54. case is_bool($value):
  55. $type = PDO::PRAM_BOOL;
  56. break;
  57. case is_null($value):
  58. $type = PDO::PRAM_NULL;
  59. break;
  60. default:
  61. $type = PDO::PRAM_STR;
  62. }
  63.  
  64. }
  65.  
  66. $this->stmt->bindValue($parm, $value, $type);
  67. }
  68.  
  69. public function execute(){
  70. return $this->stmt->execute();
  71. }
  72.  
  73. public function resultset(){
  74. $this->execute();
  75. return $this->stmt->fetchALL(PDO::FETCH_ASSOC);
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement