Guest User

Untitled

a guest
Mar 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <?php
  2.  
  3. class Database
  4. {
  5. /**
  6. * @var
  7. */
  8. private $link;
  9.  
  10. /**
  11. * Database constructor.
  12. */
  13. public function __construct()
  14. {
  15. $this->connect();
  16. }
  17.  
  18.  
  19. /**
  20. * @return $this
  21. */
  22. private function connect()
  23. {
  24. $config = require_once 'config.php';
  25.  
  26.  
  27. $dsn = 'mysql:host='.$config['host'].';db_name='.$config['db_name'].';charset='.$config['charset'];
  28.  
  29. $this->link = new PDO($dsn, $config['username'], $config['password']);
  30.  
  31. return $this;
  32. }
  33.  
  34.  
  35. /**
  36. * @param $sql
  37. * @return mixed
  38. */
  39. public function execute($sql)
  40. {
  41. $sth = $this->link->prepare($sql);
  42.  
  43. return $sth->execute();
  44.  
  45. }
  46.  
  47. /**
  48. * @param $sql
  49. * @return array
  50. */
  51. public function query($sql)
  52. {
  53. $sth = $this->link->prepare($sql);
  54.  
  55. $sth->execute();
  56.  
  57.  
  58. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  59.  
  60. if($result === false){
  61. return [];
  62. }
  63.  
  64. return $result;
  65. }
  66. }
  67.  
  68. $db = new Database();
  69.  
  70. $db->execute("INSERT INTO `user` SET `username`='Pavel', `password`='444444', `date`=".time());
Add Comment
Please, Sign In to add comment