1

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 Answers 2

2

You should add all() or one() for getting the rows

$query = new Query;

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

and obtained the models in $myModels

foreach ($myModels as $rows) {

        $cu_id = $rows['cu_id'];

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

My error is gone, but it doesn't actually loop through the query with any data. What's a good way to debug my query?
Loop without echo a value for cu_id or don't loop ?
This was close, but $rows->cu_id didn't work in this instance. The solution above with $rows['cu_id'] worked.
1

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 />";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.