PDO - 调用非对象上的成员函数fetch() [英] PDO - Call to a member function fetch() on a non-object
问题描述
我的错误:
(!)致命错误:调用非对象上的成员函数fetch \\ wamp\www\PDO\index.php第13行上
我的代码:
<?php
$ config ['db'] = array(
'host'=& ,
'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'];
}
我知道这里有很多问题,工作
感谢
以上是固定的,感谢下面的每个人。 :)现在我得到另一个错误:
注意:未定义的索引:id在C:\wamp\www\PDO\\ \\ index.php on line 7
这是我的数据库:
a href =http://d.pr/i/vcod =nofollow> http://d.pr/i/vcod
这是我的代码:
$ db = new PDO :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'];
}
>
$ query = $ db-> query(SELECT articles。title FROM articles);
尝试:
code> $ query = $ db-> query(SELECT title FROM articles);
编辑
>
while($ row = $ query-> fetch(PDO :: FETCH_ASSOC)){
echo $ row ['title '];
}
My error:
( ! ) Fatal error: Call to a member function fetch() on a non-object in C:\wamp\www\PDO\index.php on line 13
My code:
<?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'];
}
I know there are many questions like this, but none of the answers seem to work.
Thanks
EDIT:
Above is fixed, thanks to everyone below. :) Now I'm getting another error:
Notice: Undefined index: id in C:\wamp\www\PDO\index.php on line 7
Here is my database:
Here is my code:
$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'];
}
Instead of:
$query = $db->query("SELECT articles . title FROM articles");
Try:
$query = $db->query("SELECT title FROM articles");
edit
Try:
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo $row['title'];
}
这篇关于PDO - 调用非对象上的成员函数fetch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!