Looking at PDO tutorials on YouTube and PHP forums trying to figure this out.
This is my Config.php
“`
$db = new Db;
class Db
{
private $host;
private $dbName;
public $user;
public $pass;
public function Connect()
{
$this->host = ‘localhost’;
$this->dbName = ‘core’;
$this->user = ‘root’;
$this->pass = ”;
try
{
$dsn = “mysql:host=”.$this->host.”;dbName=”.$this->dbName;
$pdo = new PDO($dsn, $this->user, $this->pass);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
echo ‘Error: ‘.$e->getMessage();
}
}
}
This is Data.php
“`
require ‘Config.php’;
$data = new Data;
class Data
{
public function __construct()
{
global $db;
try
{
$pdo = new PDO($db->dsn,$db->user,$db->pass);
}
catch (PDOException $e)
{
echo ‘Error’ . $e->getMessage();
}
}
I am getting Undefined Property Db::$dsn in Data.php $dsn is a variable in Config.php, so why is it saying Undefined Property? Appreciate any help, guidance with this.