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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了