2012-05-04 24 views
0

我對此代碼有一個疑問。當我想選擇一個項目(在這種情況下是街道)時,在調試模式下一切都沒有錯誤。 (我使用AJAX來生成消息)試圖獲取MAMP中非對象的屬性

public function placeAvailable() 
    { 
     //open connectie naar database 
     include("Connection.php"); 

     $sSql= "select Street from tblPlaces 
       where Street = '".$this->m_sStreet."'"; 

     $vResult=$mysqli->query($sSql); 
     if($vResult->num_rows>0) 
     { 
      return(false); 
     } 
     else 
     { 
      return(true); 
     } 
     $mysqli->close(); 
    } 

當我想要一個額外的參數添加到我的選擇查詢我得到一個錯誤(注:試圖獲得非對象的屬性/應用程序/ MAMP/htdocs中/ Foodsquare小/資產/班/上線86 places.class.php)

$vResult=$mysqli->query($sSql); 
     if($vResult->num_rows>0) 

額外的參數我想補充的是如下因素。

$sSql= "select Street from tblPlaces 
       where Street = '".$this->m_sStreet."' AND where HouseNumber = '".$this->m_sHouseNumber."'"; 

喬希在這裏你可以看到整個代碼

<?php 

class Places { 

    private $m_sName; 
    private $m_sStreet; 
    private $m_sHouseNumber; 
    private $m_sCity; 
    private $m_sCategory; 

    public function __set($p_sProperty, $p_vValue) { 
     switch($p_sProperty) { 
      case "Name" : 
       $this -> m_sName = $p_vValue; 
       break; 
      case "Street" : 
       $this -> m_sStreet = $p_vValue; 
       break; 
      case "HouseNumber" : 
       $this -> m_sHouseNumber= $p_vValue; 
       break; 
      case "City" : 
       $this -> m_sCity = $p_vValue; 
       break; 
      case "Category" : 
       $this -> m_sCategory = $p_vValue; 
       break; 
     } 
    } 

    public function __get($p_sProperty) { 
     $vResult = null; 
     switch($p_sProperty) { 
      case "Name" : 
       $vResult = $this -> m_sName; 
       break; 
      case "Street" : 
       $vResult = $this -> m_sStreet; 
       break; 
      case "HouseNumber" : 
       $vResult = $this -> m_sHouseNumber; 
       break; 
      case "City" : 
       $vResult = $this -> m_sCity; 
       break; 
      case "Category" : 
       $vResult = $this -> m_sCategory; 
       break; 
     } 
     return $vResult; 
    } 

    public function addPlaces() 
    { 
     include_once("connection.php"); 
      $sSql = "INSERT INTO tblPlaces 
       (Name, 
       Street, 
       HouseNumber, 
       City, 
       Category) 
       VALUES 
       ('" . $mysqli -> real_escape_string($this -> m_sName) . "', 
       '" . $mysqli -> real_escape_string($this -> m_sStreet) . "', 
       '" . $mysqli -> real_escape_string($this -> m_sHouseNumber) . "', 
       '" . $mysqli -> real_escape_string($this -> m_sCity) . "', 
       '" . $mysqli -> real_escape_string($this -> m_sCategory) . "');"; 

     if (!$mysqli -> query($sSql)) 
     { 
      throw new Exception("Er is iets mis gelopen bij het toevoegen van een plaats"); 
     } 

    } 

    public function placeAvailable() 
    { 
     //open connectie naar database 
     include("Connection.php"); 
     global $mysqli; 


     //sql query selecteert straat uit tblplaces waar de straat gelijk is aan het inputveld street uit het formulier 
     $sSql= "select Street from tblPlaces 
       where Street = '".$this->m_sStreet."' AND where HouseNumber = '".$this->m_sHouseNumber."'"; 

     $vResult=$mysqli->query($sSql); 
     if($vResult->num_rows>0) 
     { 
      //Street (place) is al in gebruik 
      return(false); 
     } 
     else 
     { 
      //Hij is nog niet in gebruik 
      return(true); 
     } 
     //database sluiten 
     $mysqli->close(); 
    } 
} 
?> 

回答

0

你從來沒有定義$mysqli,你需要創建連接。

如果它在你Connection.php,你需要添加一個:

global $mysqli;

後你有,但你的查詢之前。

該錯誤表示$mysqli不是一個對象,並且沒有這樣的屬性,屬性爲query($data)

+0

Josh Thankyou,我會試試這個。但我想知道爲什麼它可以與1參數,而不是2。我的數據庫連接($ mysqli)是在connection.php文件編輯:喬希,仍然得到這個錯誤,也與全球$ mysqli; – Niels

+0

@Niels:那麼你很可能會在'm_sHouseNumber'出錯。爲了測試,嘗試添加'echo $ this-> m_sHouseNumber;',並查看是否有錯誤。如果是這樣,你可能沒有在你的類中有一個明確的屬性名稱,或者你試圖給隱式的屬性賦值(當你稍後再讀取時,這裏會失敗)。 – Josh

+0

我已經在我的課程中命名了這個屬性,在上面的原始文章中你看到了我的編輯(我把整個班級放在這篇文章中) – Niels

相關問題