PDO returning incorrect, but duplicate, data. Key's not in database.

前端 未结 3 1509
野的像风
野的像风 2020-11-27 22:57

I\'m new to using $pdo statements so might be something simple I haven\'t yet read on php.net. I\'m receiving duplicate results when querying the database.

相关标签:
3条回答
  • 2020-11-27 23:10

    seems to do the trick for me. Put this on top

    use Illuminate\Database\Events\StatementPrepared;
    

    when ever fetching include pdo::fetch_assoc

    return($pdo->fetchAll(\PDO::FETCH_ASSOC));
    
    0 讨论(0)
  • 2020-11-27 23:16

    It's not duplicates, it's just the current FETCH_MODE you're using. To get as associative keys only you need to specify as such; by default it fetches as both.

    Use like so:

    $query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
    $query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
    

    fetchAll docs
    fetch docs

    0 讨论(0)
  • 2020-11-27 23:20

    This is not duplicate data fetchAll returns data in numeric array as well as associative array.

    See the Docs

    Use this for retrieving only associative array

    return $query->fetchAll(PDO::FETCH_ASSOC);
    
    0 讨论(0)
提交回复
热议问题