What are the potential reasons for only getting the first row of results when using fetch() in a PDO statement?

When using fetch() in a PDO statement, only getting the first row of results may occur if the fetch mode is set to PDO::FETCH_ASSOC or PDO::FETCH_OBJ, which fetches only the first row by default. To retrieve all rows, you can use a loop to fetch each row one by one until there are no more rows left.

// Assuming $pdo is your PDO connection and $stmt is your prepared statement

// Set fetch mode to fetch all rows
$stmt->setFetchMode(PDO::FETCH_ASSOC);

// Fetch all rows using a loop
while ($row = $stmt->fetch()) {
    // Process each row here
    print_r($row);
}