doupapin172773
2014-05-22 08:56 阅读 15
已采纳

PDO将默认提取模式设置为FETCH_UNIQUE

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);
$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_UNIQUE);

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

Then...

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
    $PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_UNIQUE);
    

    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 http://www.php.net/manual/en/pdostatement.fetch.php

    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().

    点赞 评论 复制链接分享

相关推荐