I've been looking at this code for ages now and I cant really understand what it want from me. I keep getting the same error message no matter what I try and I've looked through other threads to no avail. The error is Fatal error: Call to a member function prepare() on a non-object in C:\wamp\www\new_serverside\sprint1.php on line 63. my code is :
<?php
include 'ConfigDB.php';
?>
<?php
if(getConnection())
{
echo "Server connection successful";
}
else
{
echo "Problem with connection";
}
ini_set('display_errors', 1);
global $dbh;
$query = $dbh->prepare("SELECT * FROM faculty");
$query->execute();
$result = $dbh->query($query);
foreach($result as $row)
{
echo $row['Name'] . "<br/>";
}
?>
The error sounds like $dbh
hasn't initialized properly. You can make sure that won't happen in the future by using a singleton class instead of a global variable.
class DB {
private $instance = NULL;
private __construct() {
// create an instance of your db handler
}
public static getInstance() {
if (!$this->instance)
$this->instance = new DB();
return $this->instance;
}
}
And use it in your code like this
$dbh = DB::getInstance();