after reading a lot of post but not getting this thing running, maybe somone can help me with this.
I am trying to include a PHP (Module.php) file which craps information from db into my index.php. This index.php also includes the file with the database connection. The problem is the included file which handles the db selects seems not to know about the PDO Object, the Script dies which this error:
Fatal error: Call to a member function prepare() on a non-object in I tried to make the PDO Object global. But unfortunately this is not working.
Thanks a lot for any help (and safe me not going crazy ...)
Tony
index.php
//DB Connection require_once ("include/db_connect_inc.php"); $request = $_GET['Controll']; switch ($request) { case 0: echo "XY"; break; case 1: global $objDb; //This file should be able to use the DB Object include("modules/Eat/Module.php"); break; }
Module.php
global $objDb; $dbSelect = $objDb->prepare( "SELECT DISTINCT ON (nummer) nummer FROM tableX " ); $dbSelect->execute(); while($row = $dbSelect->fetch(PDO::FETCH_ASSOC)) { $all = $row['nummer']; } echo "1 - " . $all; db_connect_inc.php
$strDbLocation = 'pgsql:host=localhost;dbname=test'; $strDbUser = 'root'; $strDbPassword = 'root'; try{ $objDb = new PDO($strDbLocation, $strDbUser, $strDbPassword); $objDb->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $objDb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); global $objDb; } catch (PDOException $e){ //echo 'Fehler beim Öffnen der Datenbank: ' . $e->getMessage(); print "Error!: " . $e->getMessage() . "<br/>"; }
$objDbis in the same scope for all of the files.