简体   繁体   English

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

[英]PHP undefined variable inside the same function

I've been searching for the answer for a while, but no luck: I have defined a variable inside this function and I try to use it in the same function, but PHP gives me this: 我一直在寻找答案,但是没有运气:我已经在此函数内定义了一个变量,并尝试在同一函数中使用它,但是PHP给了我这个:

Notice: 注意:
Undefined variable: query in C:\\xampp\\htdocs\\liquidity\\includes\\layout\\DBBroker.php on line 42 Warning: mysqli_query(): 未定义的变量:在第42行的C:\\ xampp \\ htdocs \\ liquidity \\ includes \\ layout \\ DBBroker.php中进行查询警告:mysqli_query():
Empty query in C:\\xampp\\htdocs\\liquidity\\includes\\layout\\DBBroker.php on line 42 第42行上的C:\\ xampp \\ htdocs \\ liquidity \\ includes \\ layout \\ DBBroker.php中的空查询

How is this possible? 这怎么可能? I define it and use it inside the same function. 我定义它并在同一函数中使用它。 The function is prikaziClanove, the one with "//here the problem starts" next to it. 该函数为prikaziClanove,其旁边带有“ //问题在这里开始”的那个。 Code: 码:

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

?>

If you look at this line: 如果您看这行:

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

That's not actually closing the string, it's using the concatenation operator. 那实际上不是关闭字符串,而是使用串联运算符。

Try this instead: 尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM