Answer the question
In order to leave comments, you need to log in
How to prevent connection to the database more than 1 time?
class DB {
public static $mysqli;
private $connect = true;
function __construct() {
if ($this->connect) {
$mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
$mysqli->set_charset("utf8");
$this->mysqli = $mysqli;
$this->connect = false;
}
}
Answer the question
In order to leave comments, you need to log in
<?php
class DB {
public static $mysqli;
private $connect = true;
private $_instance = null;
private function __construct() {
if ($this->connect) {
$mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
$mysqli->set_charset("utf8");
$this->mysqli = $mysqli;
$this->connect = false;
}
}
private function __clone(){}
public static function init(){
if($_instance === null){
self::$_instance = new self();
}
return self::$instance;
}
}
2016 is in the yard. No need to use mysqli. Use some kind of ORM thread or at least PDO.
Singleton is rather an anti-pattern, and definitely not the best solution for a database. What will you do if you need to connect to two bases?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question