Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <?php
  2. class Database{
  3. private $hostdb = "localhost";
  4. private $namedb = "bd_name";
  5. private $userdb = "root";
  6. private $passdb = "";
  7. public $pdo;
  8.  
  9. public function __construct(){
  10. if(!isset($this->pdo)){
  11. try{
  12. $link = new PDO("mysql:host=".$this->hostdb.";dbname=".$this->namedb, $this->userdb, $this->passdb);
  13. $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  14. $link->exec("SET CHARACTER SET utf8");
  15. $this->pdo = $link;
  16. }
  17. catch (PDOException $rex){
  18. die("Failed to connect with database".$rex->getMessage());
  19. }
  20. }
  21. }
  22. }
  23. ?>
  24. **********************************************
  25. Using OOP const(constant)
  26. **********************************************
  27. <?php
  28. class Database {
  29. const HOST = "localhost";
  30. const USER = "root";
  31. const PASS = "";
  32. const DB = "bd_name";
  33. public $conn;
  34.  
  35. public function __construct(){
  36. if (!isset($this->conn)) {
  37. try {
  38. $sql = new PDO("mysql:host=" . Database::HOST . ";dbname=" . Database::DB, Database::USER, Database::PASS);
  39. $sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  40. $sql->exec("SET CHARACTER SET utf8");
  41. $this->conn = $sql;
  42. } catch (PDOException $e) {
  43. die("Faied to connection with Database!".$e->getMessage());
  44. }
  45. }
  46. }
  47. }
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement