Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Database {
  5.  
  6. private $link;
  7.  
  8. public function __counstruct() {
  9. $this->connect();
  10. }
  11.  
  12. private function connect() {
  13. $user = 'animals';
  14. $pass = '****';
  15. $host='localhost';
  16. $db_name='animals';
  17. $dsn = "mysql:host=$host;dbname=$db_name";
  18.  
  19. $this->link = new PDO($dsn, $user, $pass);
  20.  
  21. return $this;
  22. }
  23.  
  24. public function execute($sql) {
  25. $sth = $this->link->prepare($sql);
  26.  
  27. return $sth->execute();
  28. }
  29.  
  30. public function query($sql) {
  31.  
  32. $sth = $this->link->prepare($sql);
  33.  
  34. $sth->execute();
  35.  
  36. $result = $sth->fetchAll(PDO::FETCH_ASSOC);
  37.  
  38. return $result;
  39. }
  40. }
  41.  
  42. $db = new Database();
  43.  
  44.  
  45. ?>
  46.  
  47. <?php
  48.  
  49. require('db.php');
  50.  
  51. $users = $db->query("SELECT * FROM 'animals'");
  52. var_dump($users);
  53.  
  54. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement