Advertisement
Guest User

Untitled

a guest
Apr 17th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. <?php
  2. /*
  3. * Mysql database class - only one connection alowed
  4. */
  5. class Database {
  6. private $_connection;
  7. private static $_instance; //The single instance
  8. private $_host = "HOST";
  9. private $_username = "USERNAME";
  10. private $_password = "PASSWORD";
  11. private $_database = "DATABASE";
  12.  
  13. /*
  14. Get an instance of the Database
  15. @return Instance
  16. */
  17. public static function getInstance() {
  18. if(!self::$_instance) { // If no instance then make one
  19. self::$_instance = new self();
  20. }
  21. return self::$_instance;
  22. }
  23.  
  24. // Constructor
  25. private function __construct() {
  26. $this->_connection = new mysqli($this->_host, $this->_username,
  27. $this->_password, $this->_database);
  28.  
  29. // Error handling
  30. if(mysqli_connect_error()) {
  31. trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error());
  32. }
  33. }
  34.  
  35. // Magic method clone is empty to prevent duplication of connection
  36. private function __clone() { }
  37.  
  38. // Get mysqli connection
  39. public function getConnection() {
  40. return $this->_connection;
  41. }
  42. }
  43. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement