Greg L
Greg L

Reputation: 468

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.

$stmt = $pdo->prepare('SELECT * FROM admin WHERE user_id = :user_id');
$stmt->bindParam(':user_id', $userid);
$stmt->execute();

$rResult = $stmt->fetchAll();
$gorResult = count($rResult);

foreach($rResult as $row) {


$data_t = '<b>Length: '. $strt_length .' </b>  |  <b>Ground: '. $strt_ground .' </b>';
$data_t .= '<span class="label label-success">'. $gorResult .' Entries Total</span>';
$data_t .= '<table class="table table-striped">';
$data_t .= '<thead>';
$data_t .= '<tr>';
$data_t .= '<th>' . $table_fields['jo_col_1_name'] . '</th>'; 
$data_t .= '<th>' . $table_fields['jo_col_2_name'] . '</th>'; 
$data_t .= '<th>' . $table_fields['jo_col_3_name'] . '</th>';
$data_t .= '</tr>';
$data_t .= '</thead>';
$data_t .= '<tbody>';
$data_t .= '<tr>';
$data_t .= '<td>' . $row['date'] . '</td>';
$data_t .= '<td>' . $row['length'] . '' . $selected . '</td>';
$data_t .= '<td>' . $row['ground'] . '' . $selected . '</td>';
$data_t .= '<td>';

if (!$del_hide) {

$data_t .= "<form method='post' action='');' />";
$data_t .= "<input type='hidden' name='primary_key' value='".$row["primary_key"]."' />";
$data_t .= '<button type="submit" name="deleteItem" value="delete" class="btn btn-link">';
$data_t .= '<span class="glyphicon glyphicon-remove"></span></button>';
} else { };
$data_t .= '<button type="image" name="image" value="image" class="btn btn-link">';
$data_t .= '<span class="glyphicon glyphicon-picture"></span></button>';
$data_t .= '</form>';
}
$data_t .= '</td>';
$data_t .= '<td>';
$data_t .= '</td>';
$data_t .= '</tr>';
$data_t .= '</tbody>';
$data_t .= '</table>';

echo $data_t;
?>

Upvotes: 2

Views: 3913

Answers (2)

Your Common Sense
Your Common Sense

Reputation: 157839

As far as I understand your long and windy story, you are doing something like this

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // fetching one row
    $data_t .= '<td>' . $row['date'] . '</td>'; // using row
    $rResult = $stmt->fetchAll(); // FETCHING REST OF ROWS
    ...
} // no more rows for the next iteration

while it have to be

$data = $stmt->fetchAll(); // fetching rows
$count = count($data); // getting count
foreach($data as $row) { // iterating over rows
    $data_t .= '<td>' . $row['date'] . '</td>'; // using row
}

Upvotes: 2

Elon Than
Elon Than

Reputation: 9765

There is only one row because you specified user_id (which, I assume, is unique key). Just remove it.

Upvotes: 0

Related Questions