Эй, я пытаюсь переделать домашнюю страницу, которую я сделал. На этот раз я хочу использовать стиль ООП. Но я всегда получаю следующее сообщение об ошибке:Не удалось получить MySQL
Статистика :: checkExistingCounter() [statistic.checkexistingcounter]: не удалось извлечь MySQL
Что я делаю неправильно? Я знаю, что инструкция prepare бессмысленна, но даже запрос, а не запрос подготовки, вообще не разыгрывается. То же ошибка: не удалось получить MySQL
Мой класс База данных:
class MySQL extends MySQLi {
private static $_instance = null;
private $host, $username, $password, $db;
public static function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct(){
$this->host = '...';
$this->username = '...';
$this->password = '...';
$this->database = '...';
$this->connect();
}
public function __destruct() {
$this->db->close();
}
private function __clone(){}
public function connect() {
$this->db = @new MySQLi($this->host, $this->username, $this->password, $this->database);
/* change character set to utf8 */
$this->db->set_charset("utf8");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
return $this->db;
}
}
Моя статистика класс:
class Statistic {
private $remote, $user_agent, $referer;
private $db;
/**
* Create Instance of MySQL
**/
function __construct($db) {
$this->db = MySQL::getInstance();
}
/**
* Check for counter today
*
* @param: string SQL
* @return: boolean (true = Counter exists, false = Counter doesnt exist)
**/
function checkExistingCounter($sql) {
$stmt = $this->db->prepare($sql);
$this->db->error;
if (!$stmt) {
echo 'Datenbankfehler';
exit;
}
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows) {
$stmt->close();
return true;
} else {
$stmt->close();
return false;
}
}
function counter() {
$sql = "SELECT ID FROM Counter WHERE Datum = CURDATE()";
$checkCounter = $this->checkExistingCounter($sql);
}
И это часть моей index.php:
$db = new MySQL();
$statistic = new Statistic($db);
$statistic->counter();
Что делает 'MySQL :: getInstance'? –
@ ExplosionPills Это очень похоже на Pattern Singleton для меня. – IMSoP