1

I don't know why user, host and password variables are undefined when I try to call the connect() method

    $app->get('/get', function () use($app){
        $db = new Db_handler;
        $db->connect();
    }
);

And here is the Db_handler class itself

    <?php
        class Db_handler{
                private $driver;
                private $host;
                private $port;
                private $schema;
                private $username;
                private $password;

                function Db_handler( $config_file = 'connection.ini' ){
                        if(!$connection_data = parse_ini_file($config_file, true)) throw new exception("No se puedo abrr el fichero de configuracion ".$config_file." .");
                        $driver = $connection_data["database"]["driver"];
                        $host = $connection_data["database"]["host"];
                        $port = $connection_data["database"]["port"];
                        $schema = $connection_data["database"]["schema"];
                        $username = $connection_data["database"]["username"];
                        $password = $connection_data["database"]["password"];

                        echo $host;
                        echo $username;
                        echo $password;
                }

                function connect(){
                        $link = mysql_connect($host, $username, $root)
                        or die('No se pudo conectar: ' . mysql_error());
                }

        }
?>

The echo of the constructor shows the variables correctly.

0

3 Answers 3

1

In your constructor and in the connect() method, use $this to preface each var name.

$this->driver = ...
$this->host   = ...

etc.

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

Comments

0

Edit your connect function. Change this line:

$link = mysql_connect($host, $username, $root)

to this:

$link = mysql_connect($this->host, $this->username, $this->password)

Variables used in function connect are local variables which are not initialized. When you use keyword $this with pointer -> you will call class variables which are already defined in constructor. So as well it's important to set class private variables inside constructor:

$this->driver = $connection_data["database"]["driver"];
$this->host = $connection_data["database"]["host"];
$this->port = $connection_data["database"]["port"];
$this->schema = $connection_data["database"]["schema"];
$this->username = $connection_data["database"]["username"];
$this->password = $connection_data["database"]["password"];

Comments

0

I think that the undifined variable is only $root. You call mysql_connect($host, $username, $root) and $root is not declared anywhere.

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.