嘿那裏即時嘗試重新編碼我做的一個主頁。這一次我想使用OOP風格。但是,我總是得到以下錯誤:無法取得MySQL
統計:: checkExistingCounter()[statistic.checkexistingcounter]:無法獲取的MySQL
我在做什麼錯?我知道準備陳述是毫無意義的,但即使只是一個查詢而不是準備陳述也根本不是什麼東西。同樣的錯誤:無法獲取的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'是做什麼的? – 2013-03-11 02:05:26
@ExplosionPills它看起來非常像我的單身模式。 – IMSoP 2013-03-11 02:18:52