PHP PDO retrieves duplicate data

后端 未结 2 855
挽巷
挽巷 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);
    
    0 讨论(0)
  • 2020-12-12 04:26

    change pdo attribute accoding to requirment. . .

     $dbConnection = new PDO( 'pgsql:host=' . $host . ';dbname=' . $database, $username, 
     $password, );
    
     $dbConnection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    

    set / change PDO attribute more detail here

    0 讨论(0)
提交回复
热议问题