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

在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.

图片转代码服务由CSDN问答提供 功能建议

出于某种原因,这不起作用。 我得到的错误是行的“Undefined index:cu_id”

  $ cu_id = $ rows ['cu_id']; 
   \  n 
 

我想我只是完全使用了foreach循环中的querybuilder错误。 有没有正确的语法帮助? 谢谢!

  $ query = new Query; 
 
 $ query-&gt; select('cu_id') - &gt; from('cu_emails') - &gt; where  (['creator_id'=&gt; $ user_id,'email'=&gt; $ email]); 
 
 
 foreach($ query as $ rows){
 
 $ cu_id = $ rows ['cu_id'  ]; 
 
 echo“CU ID:$ cu_id&lt; br /&gt;&lt; br /&gt;”; 
 
} 
   
 
 

我也是 如果有人错过了,那就是Yii 2框架。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题