Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB {
- private $connection;
- private static $_instance;
- private $host = "localhost"; // Ip Address of database if external connection.
- private $username = "james"; // Username for DB
- private $password = "james"; // Password for DB
- private $dbname = "todo"; // DB Name
- public static function getInstance(){
- if(!self::$_instance) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- private function __construct() {
- $this->connection = new mysqli($this->host, $this->username, $this->password, $this->dbname);
- if(mysqli_connect_error()){
- trigger_error("Failed to connect to MySQL DB: " . mysqli_connect_error(),E_USER_ERROR);
- }
- $sql = "CREATE TABLE IF NOT EXISTS tasks (
- id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- title VARCHAR(255) NOT NULL,
- descr VARCHAR(255) NOT NULL,
- done TINYINT(1),
- task_date DATE
- )";
- if (!mysqli_query($this->connection,$sql))
- {
- echo "Error creating a database: " . mysqli_error($this->connection);
- };
- }
- private function __clone(){}
- public function getConnection(){
- return $this->connection;
- }
- }
- $db = DB::getInstance();
- $mysqli = $db->getConnection();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement