试图在MAMP中获取非对象的属性

时间:2012-05-04 20:59:12

标签: php ajax mysqli

我对以下代码有疑问。当我想选择一个项目(在这种情况下是街道)时,一切都在调试模式下没有错误。 (我正在使用AJAX生成消息)

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();   
    }

当我想在我的select查询中添加一个额外的参数时,我收到错误(注意:在/Applications/MAMP/htdocs/foodsquare/assets/classes/places.class中尝试获取非对象的属性第86行的.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();   
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您永远不会定义$mysqli,您需要创建连接。

如果它在Connection.php中,您需要添加:

global $mysqli;

包含后,但在查询之前。

错误意味着$mysqli不是对象,并且没有这样的属性,属性为query($data)