I tried following this tutorial on PDOs, followed it exactly, yet when trying to connect on a very basic level, it’s outputing a non object error. I made a small test to determining the boolean of the $query variable and it’s coming out as false which is generating the error. Why is it doing this?:
TUTORIAL
http://www.youtube.com/watch?v=lb0NYODtGr4&feature=share&list=EC23A4AFEA46A5CB23
ERROR
Fatal error: Call to a member function fetch() on a non-object in
C:\xampp\htdocs\projects\dentaloffice\php\php_connect.php on line 16
CODE
<?php
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'dentaloffice'
);
$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname =' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$query = $db->query("SELECT appointments.ROOM FROM appointments");
if($query === false){
echo '$query variable returned false <br>';
}else{
echo '$query variable returned true <br>';
}
while ($row = $query->fetch(PDO::FETCH_ASSOC)){
echo $row['ROOM'];
}
?>
Change this line:
… into this …
In other words, remove the whitespace symbol right after
dbname, and it will work. ) PDO DSN syntax is quite strict, and the string in your example is parsed like no database was specified – hence an error you see.BTW, it’s a good practice to always check
errorInfo()in the way shown in my previous comment infalse query/statement/resultbranches of the PDO code. And now you probably see why. )