How can PHP scripts be effectively used to output the result of a select query in PDO?

When using PDO in PHP to execute a select query, the results can be retrieved using the fetch method. To output the result of a select query, you can iterate over the fetched rows and display the data as needed. This can be done by using a loop, such as a foreach loop, to iterate over the fetched rows and output the data.

<?php
// Assume $pdo is your PDO object and $query is your select query

$stmt = $pdo->query($query);

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Output the data from the fetched row
    echo $row['column_name1'] . ' - ' . $row['column_name2'] . '<br>';
}
?>