我试着按照这个关于 PDO 的教程,完全按照它,但是当尝试在一个非常基本的级别上连接时,它输出一个非对象错误。我做了一个小测试来确定 $query 变量的布尔值,它的结果是错误的,这会产生错误。为什么这样做?:
教程
http://www.youtube.com/watch?v=lb0NYODtGr4&feature=share&list=EC23A4AFEA46A5CB23
错误
致命错误:在第 16 行的 C:\xampp\htdocs\projects\dentaloffice\php\php_connect.php 中的非对象上调用成员函数 fetch()
代码
<?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'];
}
?>