Loop results PDO PHP

mysql, pdo, php

Solution

Well, you only call `$sth->fetch` once. You need to loop over the results.

while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
   echo $row['First_Name'] . ' ' . $row['Surname'] . "\n";
}

Also don't call array string indexes without braces. This way PHP checks if the key is a CONSTANT, then casts it to string. This is just bad practice and might lead to unexpected bahavior.

If this returns only one row, you probably have only one row in the database (or the result set). Show us more code!

Problem

I'm trying to fetch all records from my table on my site, I have the following ``` $sth = $conn->prepare("SELECT * FROM directory WHERE user_active != ''"); $sth->execute(); /* Exercise PDOStatement::fetch styles */ $result = $sth->fetch(PDO::FETCH_ASSOC); foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $result) { echo $result[First_Name]; } ``` Only its not returning all records, only my first, Can anybody see where I'm going wrong?

Original source