0

我对下面的代码有疑问。当我想选择一项(在本例中为街道)时,一切正常,在调试模式下没有错误。(我正在使用 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();   
    }

当我想向我的选择查询添加一个额外的参数时,出现错误(注意:尝试在第 86 行的 /Applications/MAMP/htdocs/foodsquare/assets/classes/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();   
    }
}
?>
4

1 回答 1

0

您从不定义$mysqli,您需要创建连接。

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

global $mysqli;

在您包含之后,但在您的查询之前。

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

于 2012-05-04T21:00:45.863 回答