0

I can successfully select an object, but I cannot fetch all rows from the database using the following code, can anyone see any obvious errors?

    $sql2 = "SELECT ID, Latitude, Longitude, Name FROM Countries";
    $stmt2 = $pdo->prepare($sql2);
    $stmt2->execute();

    while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
        echo $countryID = $row->ID;
        echo $countryName= $row->Name;
        echo $longitude2 = $row->Longitude;
        echo $latitude2 = $row->Latitude;
    }
2
  • You should use those arrays as: $row['key'], not $row->key. Commented Jul 14, 2015 at 9:37
  • PDO::FETCH_ASSOC fetches an assoc array, which you use as an object, use PDO::FETCH_OBJ or change $row->ID to $row['ID']. There's also no params in your query, you needn't prepare it if it really is a hard-coded query string Commented Jul 14, 2015 at 9:40

2 Answers 2

2

The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array. SO you can fetch array not object

 while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
        echo $countryID = $row['ID'];
        echo $countryName= $row['Name'];
         //Rest of the code

    }
Sign up to request clarification or add additional context in comments.

3 Comments

Ahhh arrays - thanks, will accept the answer when I can
You are welcome @Frog82 !! enjoy . Who has given downvote at least dare to give reason.
@Saty: I suspect the -1 is because, when taken literally, your answer might be seen as incorrect ("So you can fetch array not object" could be taken to mean PDO::FETCH_OBJ doesn't exist). Another reason why one might down-vote is you failed to address other (less obvious) issues like the pointless PDO::prepare call and the redundant assignments (echo $countryID = $row['ID']; vs echo $row['ID'];). I'm just guessing here, though, seeing as I didn't vote down, but I, too, dislike down-votes without a comment explaining why.
1

You have to realize that PDO::FETCH constants are on purpose. And if you want to use object notation, you have to specify PDO::FETCH_OBJ instead of ...ASSOC.

Anyway, PDO::FETCH_LAZY should be most preferred way, as it will let you use ANY notation:

while ($row = $stmt2->fetch(PDO::FETCH_LAZY)) {
    echo $row->ID;    // all
    echo $row['ID'];  // three
    echo $row[0];     // works
}

with even less memory overhead than any other method. With no memory overhead at all, to be exact.

1 Comment

Not sure about the "less memory overhead" part of your answer. Have you checked the implementation, and compared the overhead? If so, how? The PDORow instance it returns has quite a few handlers... Or are you referring to memory overhead caused by using all possible notations, in which case I agree, but the handlers do have some (negligible/micro-optimizable) overhead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.