A
Anonymous
Guest
I'm using wamp. I have Data.php and Config.php in the same folder. I have required Config.php in Data.php
Config.php
Data.php
I am getting Undefined variable for dsn, user and pass in Data.php. When I prepare a mysql statement in Data.php, I get call to member function prepare() on null. What am I doing wrong? Any help appreciated.
Config.php
Code:
private $host;
private $dbname;
private $user;
private $pass;
public function Connect()
{
$this->host = "localhost";
$this->dbname = "core";
$this->user = "root";
$this->pass = "";
$this->charset = "utf8nb4";
try
{
$dsn = "mysql:host=".$this->host.";dbname=".$this->dbname.";charset=".$this->charset;
$pdo = new PDO($dsn, $this->user, $this->pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, ERRMODE_EXCEPTION);
return $pdo;
}
catch (PDOException $e)
{
echo "Connection failed: ". $e->getMessage();
}
}
Data.php
Code:
require_once('Config.php');
Code:
public function __construct()
{
try
{
$pdo = new PDO($dsn,$user,$pass);
$pdo->connect();
}
catch (PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}
}
I am getting Undefined variable for dsn, user and pass in Data.php. When I prepare a mysql statement in Data.php, I get call to member function prepare() on null. What am I doing wrong? Any help appreciated.