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.
$dsn
__construct()
public
$this->dsn
DB
Data
global
<i>
</i>class Db extends PDO // Assuming all we really need is a PDO object
{
private $host = 'localhost';
private $dbName = 'core';
private $user = 'root';
private $pass = 'pass';
public function Connect()
{
$dsn = "mysql:host=".$this->host.";dbName=".$this->dbName;
try {
// now we can call the parent PDO constructor:
parent::__construct($dsn, $this->user, $this->pass);
$this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
}
}
}
$data = new Db();
$data
public function __construct()<br/>
{<br/>
try <br/>
{<br/>
$pdo = new PDO($dsn,$user,$pass);<br/>
} <br/>
catch (PDOException $e) <br/>
{<br/>
echo 'Error' . $e->getMessage();<br/>
} <br/>
}
>@rpjd1#1639708 Then I don't need this...in Data.php?
Data
<i>
</i>require 'Config.php'; // creates the $db object
$data = new Data($db); // pass it in to your Data object
class Data {
private $db;
public function __construct(PDO $db) {
$this->db = $db;
}
public some_data_thing() {
// Can now use $this->db to do any PDO stuff
}
}
global
Data
class Db extends PDO <br/>
{<br/>
private $host = 'localhost';<br/>
private $dbName = 'core';<br/>
private $user = 'root';<br/>
private $pass = 'pass';
<i> </i><CODE>public function Connect()
<i> </i>{
<i> </i> $dsn = "mysql:host=".$this->host.";dbName=".$this->dbName;
<i> </i> try {
<i> </i> // now we can call the parent PDO constructor:
<i> </i> parent::__construct($dsn, $this->user, $this->pass);
<i> </i> $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
<i> </i> }
<i> </i> catch (PDOException $e)
<i> </i> {
<i> </i> echo 'Error: '.$e->getMessage();
<i> </i> }
<i> </i>}</CODE>
}
require 'Config.php';<br/>
$data = new Data($db);
class Data<br/>
{<br/>
public function __construct(PDO $db)<br/>
{<br/>
$this->db = $db;<br/>
}<br/>
public some_data_thing() {<br/>
// Can now use $this->db to do any PDO stuff<br/>
}<br/>
}
__construct(PDO $db)
__contruct($dsn, $this->user, $this->pass)
DB
__construct()
PDO
...
</>
__construct(PDO $dsn, PDO $user, PDO $pass)
parent::__construct()
Db
$_ENV
<i>
</i>try
{
$dsn = "mysql:host=".$this->host.";dbName=".$this->dbName.";charset=".$this->charset;
$pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
return $pdo;
}
catch (PDOException $e)
{
echo 'Error: '.$e->getMessage();
}
<i>
</i>public function __construct()
{
$pdo = new PDO($this->dsn,$user,$pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
>@rpjd1#1640039 This line is giving me Undefined Property for dsn, Undefined variable for user and pass
<i>
</i>class Db extends PDO
{
private $host;
private $dbName;
private $user;
private $pass;
private $charset;
<i> </i>public function Connect()
<i> </i>{
<i> </i> $this->host = "localhost";
<i> </i> $this->user = "root";
<i> </i> $this->pass = "";
<i> </i> $this->dbName = "dbName";
<i> </i> $this->charset = "utf8mb4";
<i> </i>
<i> </i> try
<i> </i> {
<i> </i> $dsn = "mysql:host=".$this->host.";dbName=".$this->dbName.";charset=".$this->charset;
<i> </i> $pdo = new PDO($dsn, $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
<i> </i> return $pdo;
<i> </i> }
<i> </i> catch (PDOException $e)
<i> </i> {
<i> </i> echo 'Error: '.$e->getMessage();
<i> </i> }
<i> </i>}
}
$logic = new Logic;
$logic->MethodName();
$data = new Data;
$data->MethodName();
>@rpjd1#1640074 It's my understanding, using a connection class as above requires a constructor in the class that queries the database which is executed automatically when an object of the class is created.
Db
[code=php]
class Db extends PDO {
private $host = "localhost";
private $user = "root";
private $pass = "";
private $dbName = "dbName";
private $charset = "utf8mb4";
public function __construct() {
$dsn = "mysql:host={$this->host};dbName={$this->dbName};charset={$this->charset}";
parent::__construct($dsn, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
}
// use it:
try {
$pdo = new Db();
} catch (PDOException $e) {
die('Error: '.$e->getMessage());
}
[/code]
$pdo
$registered = $data->MethodName();
Db
$this->db
Db
0.1.9 — BETA 4.24