What is the significance of setting the FETCH_ASSOC fetch mode in PDO?
Setting the FETCH_ASSOC fetch mode in PDO allows us to retrieve data from a database query as an associative array, where the column names are used as keys. This can make it easier to work with the fetched data, as we can directly access values by their column names.
// Set the fetch mode to FETCH_ASSOC
$stmt = $pdo->prepare("SELECT * FROM table");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Accessing data using column names
foreach ($results as $row) {
echo $row['column_name'];
}