Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <?php
  2.  
  3. class DB {
  4. private $connection;
  5. private static $_instance;
  6. private $host = "localhost"; // Ip Address of database if external connection.
  7. private $username = "james"; // Username for DB
  8. private $password = "james"; // Password for DB
  9. private $dbname = "todo"; // DB Name
  10.  
  11. public static function getInstance(){
  12. if(!self::$_instance) {
  13. self::$_instance = new self();
  14. }
  15. return self::$_instance;
  16. }
  17.  
  18. private function __construct() {
  19. $this->connection = new mysqli($this->host, $this->username, $this->password, $this->dbname);
  20.  
  21. if(mysqli_connect_error()){
  22. trigger_error("Failed to connect to MySQL DB: " . mysqli_connect_error(),E_USER_ERROR);
  23. }
  24.  
  25. $sql = "CREATE TABLE IF NOT EXISTS tasks (
  26. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  27. title VARCHAR(255) NOT NULL,
  28. descr VARCHAR(255) NOT NULL,
  29. done TINYINT(1),
  30. task_date DATE
  31. )";
  32.  
  33. if (!mysqli_query($this->connection,$sql))
  34. {
  35. echo "Error creating a database: " . mysqli_error($this->connection);
  36. };
  37.  
  38. }
  39.  
  40. private function __clone(){}
  41.  
  42. public function getConnection(){
  43. return $this->connection;
  44. }
  45. }
  46.  
  47. $db = DB::getInstance();
  48. $mysqli = $db->getConnection();
  49.  
  50.  
  51. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement