Advertisement
Guest User

Untitled

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