PHP未定义变量在同一个函数内

时间:2016-01-15 23:00:46

标签: php mysqli scope

我一直在寻找答案,但没有运气:我在这个函数中定义了一个变量,我尝试在同一个函数中使用它,但是PHP给了我这个:

  

注意:
未定义的变量:第42行的C:\ xampp \ htdocs \ liquidity \ includes \ layout \ DBBroker.php中的查询   警告:mysqli_query():
第42行的C:\ xampp \ htdocs \ liquidity \ includes \ layout \ DBBroker.php中的空查询

这怎么可能?我定义它并在同一个函数中使用它。 该函数是prikaziClanove,其中包含" //此处问题开始"在它的旁边。代码:

<?php
    define ("DBHOST", "localhost");
    define("DBUSER", "standard_user");
    define("DBPASS", "standard");
    define("DBNAME", "liquidity");
    class DBBroker {
        private $dbhost;
        private $dbuser;
        private $dbpass;
        private $dbname;
        private $conn;

        function __construct() { //connects to DB and checks the connection
            $this->dbhost = DBHOST;
            $this->dbuser = DBUSER;
            $this->dbpass = DBPASS;
            $this->dbname = DBNAME;

            $this->conn = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
            if (!$this->conn) {
                die("Došlo je do greške pri konektovanju na bazu: ".mysqli_error($this->conn));
            }
        }

        /*$conn = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
        if ($conn) {
            die("Došlo je do greške pri konektovanju na bazu: ".mysqli_error($conn));
        }*/

        function chckResult($rs) { //checks the query result of query, $rs=result
            if (!$rs) {
                echo("Upit nije uspešno izvršen.");
            }
        }

        function closeConnection() { //closes the connection
            mysqli_close($connection);
        }

        function prikaziClanove($username) { //here the problem starts
            $query = "SELECT * FROM clan WHERE username='".$username."';".
            $result = mysqli_query($this->conn, $query);
            $this->chckResult($result);
            echo($result);
        }
    }

?>

1 个答案:

答案 0 :(得分:7)

如果你看这一行:

$query = "SELECT * FROM clan WHERE username='".$username."';".

实际上并没有关闭字符串,而是使用连接运算符。

请改为尝试:

$query = "SELECT * FROM clan WHERE username='".$username."'";