Advertisement
Miqueel97

Untitled

Nov 10th, 2021
97
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 Database
  4. {
  5. private $_connection;
  6. private static $_instance; //The single instance
  7. private $_host = "localhost";
  8. private $_username = "root";
  9. private $_password = "";
  10. private $_database = "m07";
  11.  
  12. /*
  13. Get an instance of the Database
  14. @return Instance
  15. */
  16. public static function getInstance()
  17. {
  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. {
  27. $this->_connection = new mysqli($this->_host, $this->_username, $this->_password, $this->_database);
  28.  
  29. // Error handling
  30. if (mysqli_connect_error()) {
  31. trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(), E_USER_ERROR);
  32. }
  33. }
  34.  
  35. // Magic method clone is empty to prevent duplication of connection
  36. private function __clone()
  37. {
  38. }
  39.  
  40. // Get mysqli connection
  41. public function getConnection()
  42. {
  43. return $this->_connection;
  44. }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement