dongzha0149 2016-09-28 19:50
浏览 109
已采纳

在Yii 2中使用带有Query Builder的foreach循环

For some reason this isn't working. The error I get is "Undefined index: cu_id" for the line

$cu_id = $rows['cu_id'];

I think I'm just totally using the querybuilder wrong with the foreach loop. Any help with the proper syntax for this? Thanks!

$query = new Query;

            $query->select('cu_id')->from('cu_emails')->where(['creator_id' => $user_id, 'email' => $email]);


    foreach ($query as $rows) {

            $cu_id = $rows['cu_id'];

            echo"CU ID: $cu_id<br /><br />";

    }

Also I'm on the Yii 2 framework in case anyone missed that.

  • 写回答

2条回答 默认 最新

  • douyi8732 2016-09-28 20:05
    关注

    You query not run.

    $query->all()
    

    and then foreach records or

    $query->one()
    

    and get data from one record

    $query = new Query;
    $query->select('cu_id')->from('cu_emails')->where(['creator_id' => $user_id, 'email' => $email])
    $results = $query->all();
    
    foreach ($results as $rows) {
            $cu_id = $rows['cu_id'];
            echo"CU ID: $cu_id<br /><br />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭