1

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?

3
  • Can you present the invoking code? Commented Jun 8, 2011 at 8:14
  • 2
    Why reinvent the wheel? Consider using PDO Commented Jun 8, 2011 at 8:17
  • Hm, I didn't know about PDO. I will try to use it. Thank you ) Commented Jun 8, 2011 at 8:39

2 Answers 2

5

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.

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

Comments

4

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.

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.