[Solved] Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\sideProjects\loginTut\db.php on line 20 [closed]


You forgot to assign a value to $conn from the return value of DBConnect(), eg

this

DBconnect($config);

should be

$conn = DBConnect($config);

You also don’t need to use global $conn in your script as it will be in scope.

I’d also recommend not catching the exception from the PDO constructor. How else will you know something went wrong? echo-ing the exception message does not deal with the error.

And yet one more thing, put session_start() at the top of your script or at least above the include statements. If any of them output data to the browser, you’ll get the old “headers already sent” error.

Update

Now that I’ve looked a little closer at your question, why are you wrapping the PDO methods in user functions like query()? You aren’t adding anything here so why not just use the PDO object directly?

0

solved Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\sideProjects\loginTut\db.php on line 20 [closed]