构造函数中定义的未定义变量

时间:2012-10-24 15:06:17

标签: php

嗨我似乎在我的代码上出现错误,似乎说我没有initialez我的变量但是veriabile在构造函数中被初始化:

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

这是我的代码:

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

在第一个if语句中抛出错误。我该如何解决?

2 个答案:

答案 0 :(得分:3)

替换

$this->$conn

$this->conn
当你有错误时,

就行了。

答案 1 :(得分:2)

$this->$conn

这是错的,应该是

$this->conn