我的错误:
( ! ) Fatal error: Call to a member function fetch() on a non-object in C:\wamp\www\PDO\index.php on line 13
我的代码:
<?php
$config['db'] = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'learnpdo'
);
$db = new PDO('mysql:host='.$config['db']['host'].';dbname'.$config['db']['dbname'], $config['db']['username'], $config['db']['password']);
$query = $db->query("SELECT articles . title FROM articles");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['title'];
}
我知道有很多这样的问题,但似乎没有一个答案有效。
谢谢
编辑:
上面是固定的,感谢下面的每一个人。:) 现在我遇到了另一个错误:
Notice: Undefined index: id in C:\wamp\www\PDO\index.php on line 7
这是我的数据库:
这是我的代码:
$db = new PDO('mysql:host=localhost;dbname=learnpdo;charset=UTF-8', 'root', '');
$query = $db->query("SELECT `articles`.`title` FROM `articles`");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['id'];
}