Guest User

Untitled

a guest
Oct 26th, 2018
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. <?php
  2. $servername ='localhost';
  3. $username = 'root';
  4. $password = '12345';
  5. $dbname = 'test';
  6. try {
  7. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  8. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. do stuff ...
  10. }
  11. catch(PDOException $e) {
  12. echo $e->getMessage();
  13. }
  14.  
  15. class Database {
  16. // DB Params
  17. private $host = 'localhost';
  18. private $db_name = "test";
  19. private $username = "root";
  20. private $password = "12345";
  21. private $conn;
  22.  
  23. // DB Connect
  24. public function connect() {
  25. $this->conn = null;
  26. echo "connect() - ";
  27. try {
  28. $this->conn = new PDO('myqsl:host=' . $this->host . ';dbname=' . $this->db_name, $this->username, $this->password);
  29. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  30.  
  31.  
  32. } catch(PDOException $e) {
  33. echo 'HEY! Connection Error: ' . $e->getMessage();
  34. }
  35.  
  36. return $this->conn;
  37. }
  38. }
Add Comment
Please, Sign In to add comment