Goodmorning everyone. I coded a class that return me:
Notice: Undefined variable: db in /Applications/MAMP/htdocs/Test Vari/index.php on line 12
Fatal error: Call to a member function row() on null in /Applications/MAMP/htdocs/Test Vari/index.php on line 12
This is the first part of the php file:
session_start();
$_SESSION['ID'] = 1;
require_once 'pass/password.inc.php'; /*Here's the DB Class*/
I extended a class into another, here's the code:
class pg extends DB{
public $id;
function __construct(){
parent::__construct();
$this->id = $_SESSION['ID'];
}
public function pgname(){
$rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
return($rs);
}
}
$pg = new pg();
print_r ( $pg->pgname());
The $db->row() is declared in DB class that I extended, and I know for sure that's working. The DB class isn't initialized, and when I do, the error is the same, this is how I do it:
class pg extends DB{
public $id;
public $db;
function __construct(){
parent::__construct();
$this->db = new DB();
$this->id = $_SESSION['ID'];
}
public function pgname(){
$rs = $db->row("SELECT CONCAT(Nome, ' ', Cognome) FROM Personaggio WHERE id = :id",array("id"=>$this->id));
return($rs);
}
}
The Fatal Error will disappear when I delete braces in print_r($pg->pgname);