PDO - Call to a member function fetch() on a non-object

21,352

Solution 1

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'];
}

Solution 2

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
Share:
21,352
user1453094
Author by

user1453094

Updated on August 26, 2020

Comments

  • user1453094
    user1453094 over 3 years

    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
    user1453094 over 11 years
    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.
  • octern
    octern over 11 years
    The syntax is unusual, but I just tested it and it works fine (assuming that title is actually a column in articles).
  • Sammaye
    Sammaye over 11 years
    @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.
  • Jonathan
    Jonathan over 11 years
    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.
  • user1453094
    user1453094 over 11 years
    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
    user1453094 over 11 years
    Thanks, your answer was very helpful. But I'm getting another error now: stackoverflow.com/questions/12126193/…
  • Sammaye
    Sammaye over 11 years
    @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?
  • Jonathan
    Jonathan over 11 years
    @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.
  • Sammaye
    Sammaye over 11 years
    @user1453094 heh edited back about 1 sec before you wrote that :P
  • user1453094
    user1453094 over 11 years
    @Sammaye Sorry, my eyes must be deceiving me, I don't see an edit. :S And haha. :P
  • Sammaye
    Sammaye over 11 years
    @user1453094 There should be a grey bar aboive my answer, if not refresh; you might be using a non html5 browser
  • user1453094
    user1453094 over 11 years
    @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
  • Jonathan
    Jonathan over 11 years
    @user1453094 This code does something else. Try my updated answer to do what you really said with your code.
  • Sammaye
    Sammaye over 11 years
    @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.
  • user1453094
    user1453094 over 11 years
    Oh dayum, that's embarrassing, sorry about that, stupid error haha