Advertisement
Guest User

Untitled

a guest
Apr 6th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Handler{
  4. protected $host;
  5. protected $db_name;
  6. protected $db_username;
  7. protected $db_password;
  8. protected $db_port;
  9.  
  10. public function __construct( $db_host, $db_name, $db_username, $db_password, $port = 3306 ){
  11. $this->host = $db_host;
  12. $this->db_name = $db_name;
  13. $this->db_username = $db_username;
  14. $this->db_password = $db_password;
  15. $this->db_port = $port;
  16. }
  17.  
  18. // connect method to talk to the database.
  19. public function connect(){
  20. try{
  21. $this->dbh = new PDO( 'mysql: host=' . $this->host . ';port=' . $this->db_port . ';dbname=' . $this->db_name, $this->db_username, $this->db_password );
  22. $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  23. }
  24. catch ( PDOException $e) {
  25. $error = "Error!: " . $e->getMessage() . '<br />';
  26. echo $error;
  27. return FALSE;
  28. }
  29. return TRUE;
  30. }
  31.  
  32. }
  33.  
  34.  
  35. $short_connect = new DB_Handler('hostname', 'database_name', 'user_name', 'password', 'port');
  36.  
  37. $r = $short_connect->connect();
  38. var_dump($r);
  39. die;
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement