What are the potential design flaws in using the query() and fetch() functions separately in PHP?

When using the query() and fetch() functions separately in PHP, there is a potential design flaw where the query may be executed multiple times unnecessarily. To solve this issue, you can store the result of the query in a variable and then use fetch() on that variable to fetch the data without re-executing the query.

// Execute the query and store the result in a variable
$result = $pdo->query("SELECT * FROM table_name");

// Fetch the data from the result variable
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
    // Process the data
}