1

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:

Notice:
Undefined variable: query in C:\xampp\htdocs\liquidity\includes\layout\DBBroker.php on line 42 Warning: mysqli_query():
Empty query in C:\xampp\htdocs\liquidity\includes\layout\DBBroker.php on line 42

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

?>

1 Answer 1

7

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."'";
Sign up to request clarification or add additional context in comments.

1 Comment

Have I really done that? I can't believe myself. And it wasn't even the concatenation dot. It was a period. I finished a sentence :D Thank you for noticing, I failed miserably. I will accept you answer... in 8 minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.