0

Hi I seem to be getting an error on my code that seems to say that I have not initialez my variable but th veriabile is initialized in the constructor:

Notice: Undefined variable: conn in D:\Program Files\xampp\htdocs\Tutorials\Login\classes\mysql.php on line 18

Fatal error: Cannot access empty property in D:\Program Files\xampp\htdocs\Tutorials\Login\classes\mysql.php on line 18

And this is my code:

<?php
require_once('includes/constants.php');
    class Mysql{

        private $conn;

        function __construct() {
            $this->conn = new mysqli(DB_SERVER , DB_USER , DB_PASSWORD , DB_NAME) or 
                   die('There was a problem connecting to the database');
        }
        function verify_Username_and_Pass($un , $pwd){

            $query = "SELECT * 
                      FROM users
                      WHERE username = ? AND password= ?
                      LIMIT 1";

            if($stmt = $this->$conn->prepare($query)){ //this is where the error points
                $stmt->bind_param('ss', $un, $pwd);
                $stmt->execute();

                if($stmt->fetch()){
                    $stmt->close;
                    return true;
                }
            }   
        }
    }
?>

The error is thrown at the first if statement.How can I solve it?

2 Answers 2

3

Replace

$this->$conn

with

$this->conn

on the line when you have the error.

Sign up to request clarification or add additional context in comments.

Comments

2
$this->$conn

This is wrong, it should be

$this->conn

Comments

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.