2

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:

http://d.pr/i/vcod

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'];
}
user1453094
  • 373
  • 2
  • 7
  • 13
  • 2
    This error almost always means that your query didn't work the way you wanted it to, and as a result `$query` is `null` (or false?) and not a query result object. – octern Aug 25 '12 at 22:41
  • Thanks for the fast response. The problem is, I don't know what's wrong with the query. The articles table exists as does the titles column. Sorry for the nuisance, this is my first time using PDO. – user1453094 Aug 25 '12 at 22:43
  • I posted a couple of basic debugging suggestions in my answer, below. – octern Aug 25 '12 at 22:46

3 Answers3

6

PDO::query() returns false (which obviously is a non-object) if the query fails. So that's your problem. Your query has an error.

I recommend you to set the error mode in PDO.

$db->setAttribute(PDO::ATTR_ERRMODE, PDO:ERRMODE_EXCEPTION);

and then catch the error to see what is wrong with your query.

The different error modes that exist is PDO::ERRMODE_SILENT, PDO::ERRMODE_WARNING and PDO:ERRMODE_EXCEPTION. You can read about them in the PHP manual on PDO. Knowing how to debug when writing SQL is important and the key to not having to ask these kind of questions.

About your other problem

You gotta select the id column if you want to use it.

SELECT * FROM articles

or

SELECT id, title FROM articles
Jonathan
  • 2,968
  • 3
  • 24
  • 36
  • Thanks for the fast response. The problem is, I don't know what's wrong with the query. The articles table exists as does the titles column. Sorry for the nuisance, this is my first time using PDO. – user1453094 Aug 25 '12 at 22:43
  • Add error reporting so you can see the actual SQL error. Without seeing your DB schema we can't know for sure what's wrong with your query. – Jonathan Aug 25 '12 at 22:49
  • Oh dayum, that's embarrassing, sorry about that, stupid error haha – user1453094 Aug 25 '12 at 23:05
2

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'];
}
Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • The syntax is unusual, but I just tested it and it works fine (assuming that title is actually a column in articles). – octern Aug 25 '12 at 22:45
  • @octern Thought it might work, didn't have a SQL console to hand though and wasn't sure if the server handled that and user1453094 Hmmmm I would turn on error reporting for this, also run the query directly against the DB just to be sure. – Sammaye Aug 25 '12 at 22:49
  • Thanks for all your help, very helpful so far. I changed the db line to `$db = new PDO('mysql:host=localhost;dbname=learnpdo;charset=UTF-8', 'root', '');` but now I'm getting Undefined index: id in C:\wamp\www\PDO\index.php on line 14 – user1453094 Aug 25 '12 at 22:51
  • @user1453094 What code do you have there? Most likely you are trying to access id from those rows when you are only returning title, can you edit your question? – Sammaye Aug 25 '12 at 22:55
  • @user1453094 There's nothing wrong with that line. You error tells you you try to access an index that doesn't exist in an array. – Jonathan Aug 25 '12 at 22:58
  • @user1453094 heh edited back about 1 sec before you wrote that :P – Sammaye Aug 25 '12 at 23:01
  • @Sammaye Sorry, my eyes must be deceiving me, I don't see an edit. :S And haha. :P – user1453094 Aug 25 '12 at 23:01
  • @user1453094 There should be a grey bar aboive my answer, if not refresh; you might be using a non html5 browser – Sammaye Aug 25 '12 at 23:02
  • @Sammaye Oh! Silly me! And it works! I have no idea what was wrong, but thanks a bunch, really appreciated it, sorry you had to put up with my "noobishness", haha – user1453094 Aug 25 '12 at 23:04
  • @user1453094 This code does something else. Try my updated answer to do what you really said with your code. – Jonathan Aug 25 '12 at 23:05
  • 1
    @user1453094 You sql query was only returning title so when you tried to get the id of that row it wasnt there and thats what PHP was basically saying :) glad to have helped. – Sammaye Aug 25 '12 at 23:05
0

This error almost always means that your query didn't work the way you wanted it to, and as a result $query is null (or false?) and not a query result object. Try echoing db->errorinfo() and see if it tells you what went wrong.

You can also try pasting the text of your query into mysql directly and seeing what the result is. If that appears to work correctly, then the problem might be with your database connection code.

octern
  • 4,825
  • 21
  • 38
  • Thanks, your answer was very helpful. But I'm getting another error now: http://stackoverflow.com/questions/12126193/pdo-call-to-a-member-function-fetch-on-a-non-object/12126237#comment16216189_12126237 – user1453094 Aug 25 '12 at 22:54