嘿那里即时尝试重新编码我做的一个主页。这一次我想使用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