2014-05-22 08:56 阅读 15


I don't know why I cannot set the default fetch mode of PDO to FETCH_UNIQUE

Here is how I create the PDO object and make my request :

$PDO = new PDO($mysql,$login,$password);

$prep = $PDO->prepare($selectQuery);


This is not working (it does not take into account the previous setAttribute()) :

$result = $prep->fetchAll(); 

This is working, but I don't want to have to write "PDO::FETH_UNIQUE" for every requests :

$result = $prep->fetchAll(PDO::FETCH_UNIQUE); 

What am I doing wrong ? My goal is to get the result array indexed by the primary key of the selected table, exactly like on this thread : Is there a way to fetch associative array grouped by the values of a specified column with PDO?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    doucan1996 doucan1996 2014-05-22 09:14

    This does not work because, PDO::FETCH_UNIQUE is not a valid constant for PDO::ATTR_DEFAULT_FETCH_MODE. See here the valid fetch mode constants

    It seems that there is no way to enable PDO:FETCH_UNIQUE using $PDO->setAttribute(). You should pass it explicitly in the call to fetchAll().

    点赞 评论 复制链接分享