Search code examples
phpdatabasevariables

Variable undefined when it should be visible(defined)


I have a bit of a problem when I'm trying to connect to my database in my code, I create a variable which will be used to connect me through functions into my database..

The problem is the functions as I understand don't recognize the variable in the code, and says that it is undefined, I don't understand why it complains when through my logic it shoudn't..

Code:

 <?php
class database{
    private $connected = FALSE;
    public function connect(){
        if(!$connected){
            mysql_connect('url', 'user', 'pw');
            @mysql_select_db('db_11765278') or die('Database not found');
            $connected = TRUE;
        }
    }
    public function close(){
        if($connected){
            mysql_close();
            $connected = FALSE;
        }
    }
    public function query($sqlQuery){
        if(!$connected){
            database::connect();
        }
        $result = mysql_query("$sqlQuery");
        if($connected){
            database::close();
        }
        return $result;     
    }
    public function megaQuery($sqlQuery){
        $result = mysql_query("$sqlQuery");
        return $result;
    }
}

?>

Regards Alexein!


Solution

  • You are not referring to the class property with $this->connected as you should be doing. $connect refers to a local variable, not a property.

    Why not freshen up your knowledge a bit?