null variable in Php class

2019-08-05 10:34发布

问题:

I'm very new in php, so I have a very studid problem. I try to write class that will help me with work with DB. Code here:

<?php
    class DbHelper{
        private $databaseURL;
        private $databaseUName;
        private $databasePWord;
        private $databaseName;
        private $nameOfDbWithWorkers;
        private $connection;

        function __construct($dbURL, $dbUserName, $dbPword, $dbName, $nameOfDbWithWorkers){
            $this->databaseURL = $dbURL;
            $this->databaseUName = $dbUserName;
            $this->databasePWord = $dbPword;
            $this->databaseName = $dbName;
            $this->nameOfDbWithWorkers = $nameOfDbWithWorkers;
        }

        function setConnectionToDb(){
            $connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB"); 
            mysql_select_db($this->databaseName, $connection)or die ("Error while connecting to database");
        }

        function getUser($login, $pass){
            $query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';"; 
            $queryResult = $this->getDataFromDbByQuery($query);
            echo $queryResult;
            if ((mysql_affected_rows($connection) == 1)){
                $meta = mysql_fetch_assoc($queryResult);
                if ($meta['type']=='admin'){
                    return 'admin';
                }
                if ($meta['type']=='user'){
                    return 'user';
                }
                else{
                    return 'nomatch';
                }
            }
            else{               
                 return 'nomatch';
            }           
        }

        function getDataFromDbByQuery($query){
            $this->setConnectionToDb();
            $result = mysql_query($query);
            mysql_close($connection);
            return $result;
        }
}
?>

I invoke getUser method, but have a problem - $connection = null in getUser method and in getDataFromDbByQuery. Why so?

回答1:

Because a simple variable is always local (except you make it global using global, but don't get used to this ;)). What you are probably looking for is a property. You already defined private $connection, but just with $connection within your methods you don't use it, but you are trying to access an undefined local variable

    function setConnectionToDb(){
        $this->connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB"); 
        mysql_select_db($this->databaseName, $this->connection)or die ("Error while connecting to database");
    }

    //..
    function getUser($login, $pass){
        $query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';"; 
        $queryResult = $this->getDataFromDbByQuery($query);
        echo $queryResult;
        if ((mysql_affected_rows($this->connection) == 1)){
            // ...
        }
    }

and so on. Just replace every $connection with $this->connection.



回答2:

Try replacing $connection with $this->connection.

$connection refers to a locally scoped variable -- i.e. one that will not be available outside the function you're currently in. $this->connection refers to the member variable of the class that you've defined with private $connection.