doufuhao8085 2015-11-25 09:23
浏览 53
已采纳

数组到字符串转换SYmfony Sql

I have an sql request:

 $listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
            $listUsers->execute();
            $users=$listUsers->fetchColumn();

but fetchColumn return just one result, and I want a set of list user id to use it in the next request.

 $listPush=$connection->prepare("select id from Table_name where id_user in (?));
 $listPush->bindValue(1, $users);
            $listPush->execute();
            $idpushs=$listPush->fetchColumn();

but this return just one result. Any Idea to replace fetchColumn by other request or using doctrince.

  • 写回答

1条回答 默认 最新

  • doulouxun6756 2015-11-25 09:42
    关注

    With your logic, you can simply fetch all your users and implode it.

    $listUsers=$connection->prepare("SELECT u.id as userid From user u , account a where (a.id_parent=$id_account OR  a.id=$id_account) and u.id_account=a.id ");
    $listUsers->execute();
    $users=$listUsers->fetchAll();
    $userIds = array();
    foreach ($users as $user) {
       $userIds[] = $user['userid'];
    }
    $users = implode(',', $userIds); //that hurts me so hard to code like this with symfony :'(
    

    Fetch colum will only return the column of the next row. So for your second piece of code, you can loop over results.

    But, if you're are using a framework like symfony, you have many other ways to make it cleaner. Just check out the symfony doc to use repository relations and more generally doctrine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败