Monday, 9 September 2013

fetch(PDO::FETCH_ASSOC only returning one row

fetch(PDO::FETCH_ASSOC only returning one row

Changing over from what is now depreciated mysql code, over to PDO. This
code is supposed to output all the values within the table. Here is the
code:
$stmt = $pdo->prepare('SELECT * FROM admin WHERE user_id = :user_id');
$stmt->bindParam(':user_id', $userid);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
and the code that calls the fields are:
$data_t .= '<td>' . $row['date'] . '</td>';
$data_t .= '<td>' . $row['length'] . '' . $selected . '</td>';
$data_t .= '<td>' . $row['ground'] . '' . $selected . '</td>';
For some reason, rather than output all of the matching values selected,
only one is spit out. Now I also make use of a count function to display
how many entries a person has made, and that shows one entry less than is
actually in the database. (meaning it displays a numerical count of 4 if 5
exist) Here is that code:
$rResult = $stmt->fetchAll();
$gorResult = count($rResult);
I have tried using fetchAll() with this code and that returns nothing at
all. I know I must be missing something here, and it's likely simple for
someone with a fresh brain. Again the issue is this call only outputs one
row, rather than all matching rows.

No comments:

Post a Comment