How can the fetch() function in PHP affect the output of data from a database query?

When using the fetch() function in PHP to retrieve data from a database query, it is important to handle the data properly to avoid any unexpected output. One common issue is that fetch() returns data in different formats depending on the fetch style used. To ensure consistent output, it is recommended to specify the fetch style explicitly, such as using PDO::FETCH_ASSOC to fetch data as an associative array.

// Example code snippet using fetch() with PDO::FETCH_ASSOC fetch style
$stmt = $pdo->query('SELECT * FROM table');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    // Process data here
}