PHP PDO retrieves duplicate data

后端 未结 2 858
挽巷
挽巷 2020-12-12 03:50

I have this situation with PHP (PDO);

I\'m implementing a method that retrieves data from MySql and does it well, but the problem is that duplicate data recovered as

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 04:15

    By default PDO uses the PDO::FETCH_BOTH as its fetch result. Meaning that it maps every column to its name and to an 'integer' column name.

    Check the fetch_style param= http://php.net/manual/en/pdostatement.fetch.php

    For your expected result you will need to use the PDO::FETCH_ASSOC attribute. You can or set this as the default FETCH_MODE on the pdo object as follows:

    $this->db->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
    

    Or you can specify it for every fetch cal:

    $value = $statement->fetchAll(PDO::FETCH_ASSOC);
    

提交回复
热议问题