Home >Database >Mysql Tutorial >Why is My PDO Prepared Statement fetch() Returning Double Results?
PDO Prepared Statement fetch() Returns Double Results
You're not the only developer who has encountered unexpected results when using PDO prepared statements with the fetch() method. To resolve this issue, you need to understand how fetch() behaves with different fetch styles.
By default, fetch() returns both an associative array (indexed by column name) and a numeric array (indexed by column number). In your case, this means that each column from each row in the table is echoed out twice.
To avoid this, you should specify a specific fetch style when calling fetch(). You can do this by passing one of the PDO::FETCH_* constants as the second argument to the fetch() method.
Associative Array Only:
<code class="php">while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_ASSOC)) { // Your code here }</code>
Numeric Array Only:
<code class="php">while ($rows_get_rows = $result_get_rows->fetch(PDO::FETCH_NUM)) { // Your code here }</code>
By specifying the fetch style, you can control how the data is returned from fetch() and prevent unwanted duplication.
The above is the detailed content of Why is My PDO Prepared Statement fetch() Returning Double Results?. For more information, please follow other related articles on the PHP Chinese website!