Advertisement
Guest User

Untitled

a guest
Oct 7th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4. private $_connection;
  5. private static $_instance;
  6. private $_host = "IP";
  7. private $_username = "USERNAME";
  8. private $_password = "PASS!";
  9. private $_database = "DATABASENAME";
  10.  
  11. public static function getInstance()
  12. {
  13. if(!self::$_instance)
  14. {
  15. self::$_instance = new self();
  16. }
  17. return self::$_instance;
  18. }
  19.  
  20. private function __construct()
  21. {
  22. $this->_connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
  23.  
  24. if(mysqli_connect_error())
  25. {
  26. trigger_error("Failed to connect to MySQL: " . mysql_connect_error(), E_USER_ERROR);
  27. }
  28. }
  29.  
  30. private function __clone() { }
  31.  
  32.  
  33. public function getConnection()
  34. {
  35. $this->_connection->query('SET CHARACTER SET utf8');
  36. return $this->_connection;
  37. }
  38. }
  39.  
  40. <?php
  41. require_once('database.php');
  42.  
  43. $db = Database::getInstance();
  44. $db = $db->getConnection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement