Feeling stupid... PHP variable scope
-
Hi all, Years since the last PHP code I wrote... I want to access a MYSQL database. It works... But, now I want to be able to use that database connection/pointer from all my other files... In my database connection file I have:
$BDD = new cBDD();
class cBDD
{
var $BDDptr;function \_\_construct() { $servername = "xxx.xxx.xxx.xxx:yyyy"; $username = "user"; $password = "pwd"; $dbname = "databaseName"; $this->BDDptr = new mysqli($servername, $username, $password, $dbname); echo(!$this->BDDptr); if ($this->BDDptr->connect\_error) { die("connection failed: " .$this->BDDptr->connect\_error); } $this->BDDptr->set\_charset("utf8"); $this->BDDptr->query('SET NAMES utf8'); } function consulta($sql) { $result = $this->BDDPtr->query($sql); return $result; }
}
At construction time it's clear I've accessed BDDPtr as SetCharset and Set names queries worked ok. But I can't make it work from any other file, even in the beginning of each file I have:
include('/bdd.php');
From the external files I've tried:
$BDD->$BDDPtr->query(...
cBDD::$BDDPtr->query(...But when I add this line to my code the web page disappears... What I'm doing wrong? The error I'm getting is:
Fatal error: Uncaught Error: Call to a member function query() on null in /connexio.php:33 Stack trace: #0 /login.php(42): cBDD->consulta('SELECT myField...') #1 {main} thrown in /connexio.php on line 33
Line 33 has: $result = $this->BDDPtr->query($sql); And $sql is: "$sql = "SELECT myField FROM tUsuaris WHERE login = $usuari";" Thank you very much!