PDO Prepared Statement Fetch() Duplicating Results
A PHP script using PDO prepared statements and fetch() is outputting duplicate data to a CSV file. Each row from the database is echoed twice, resulting in doubled column values.
Previously, when not using PDO, the query worked as expected. Therefore, the issue may lie in the usage of fetch().
By specifying the desired data retrieval mode when calling fetch(), the duplicate results issue can be resolved. There are two options:
To implement this, modify the code as follows:
<code class="php">while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_ASSOC)) { $csv .= '"'.join('","', str_replace('"', '""', $rows_get_rows))."\"\n"; }</code>
The above is the detailed content of Why is my PDO Prepared Statement Fetch() Duplicating Results?. For more information, please follow other related articles on the PHP Chinese website!