0

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/>"; } 
1
  • 1
    Your use of global is completely incorrect. There are no functions in the code you have provided, $objDb is in the same scope for all of the files. Commented Aug 15, 2012 at 14:08

1 Answer 1

1

As you don't seem to use any functions, your variable is already set in the global scope, so you should get rid of all the global $objDb; lines.

That should solve the problem as long as there is no error in the first 3 lines where you connect to the database.

Apart from that I would use OOP / classes and use dependency injection to make sure my Module class is always provided with the stuff it needs (a db connection in this case).

Sign up to request clarification or add additional context in comments.

5 Comments

mhhh .... The global lines where only the last try after hours. I tried it without this before. And it didn´t work. I have checked the connection this seems to be correct.
@t Book What error do you get without any of the global lines? You can start with adding a var_dump($objDb); after each of the db connection lines (new PDO until after the last setAttribute).
Hi jeroen, the error stays the same if I kick everyhing global out Fatal error: Call to a member function prepare() on a non-object in
thats the var_dump object(PDO)#1 (0) { } object(PDO)#1 (0) { } object(PDO)#1 (0) { }
okay, sun is shining I found this singelton class. stackoverflow.com/questions/3010097/… which I don´t understand but it´s working meand good for learning! thanks to everybody

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.