2013-03-11 19 views
0

嘿那裏即時嘗試重新編碼我做的一個主頁。這一次我想使用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(); 
+0

'MySQL :: getInstance'是做什麼的? – 2013-03-11 02:05:26

+0

@ExplosionPills它看起來非常像我的單身模式。 – IMSoP 2013-03-11 02:18:52

回答

1

你似乎陷入了困境,實施了兩套競爭編碼模式:

  • MySQL類都擴展MySQLi(即任何MySQL對象也是一個MySQLi對象),並在其私有變量「代表」到MySQLi實例$db
  • Statistic類需要的MySQL一個實例在其構造函數(「依賴注入」),但然後忽略它,並要求MySQL類爲「單例」實例。

你需要更仔細地什麼這些模式是讀了起來,並決定一個或另一個在每種情況下(繼承代表團,依賴注入單身)。

目前,您的代碼將做到以下幾點:

  1. 創建一個新的MySQL對象(這也是一個MySQLi對象,但尚未初始化任何特定數據庫的連接,因爲你已經不叫在MySQL構造parent::__construct()
  2. ,設置$this->host
  3. connect()方法,創建一個新MySQLi對象,並向它的主機等
  4. 保存這個對象爲$this->db,它永遠只能在析構函數($this->db->close()
  5. 回報從connect()MySQLi對象,但沒有在__construct()正在尋找該返回值
  6. 回在外部編碼,在MySQL對象引用傳遞給Statistic
  7. 構造則忽略此的構造函數,並調用辛格爾頓方法MySQL::getInstance()代替
  8. getInstance()方法(因爲這是它第一次被調用)將創建第二個MySQL對象,重複步驟1至5
  9. 此第二MySQL對象將在Statistics對象
  10. checkExistingCounter方法試圖使用$this->db作爲MySQLi連接上保存爲$this->db,但MySQL對象從未連接到任何數據庫,所以你得到一個錯誤。 (有一個連接的連接,如果它不是私人的,你可以訪問它作爲$this->db->db。還有一個踢,以及在第2步創建,但你不能再訪問,因爲你忽略它在第7步。)
+0

感謝您指出。嗯,我想我需要閱讀更多關於單身模式。 – JPM 2013-03-11 02:41:08