Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Feeling stupid... PHP variable scope

Feeling stupid... PHP variable scope

Scheduled Pinned Locked Moved Database
databasephpmysqldata-structurestesting
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joan M
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups