from('{{%ybwm_user_coupon}}') ->where(['state' => 2, 'selfCode' => 0]) ->all(); if(!empty($data)){ foreach ($data as $key => $v){ $code = rand(11111111,9999999); $res = Yii::$app->db->createCommand()->update('{{%ybwm_user_coupon}}', ['selfCode'=>$code], 'id=:id', ['id' =>$v['id']])->execute(); echo empty($res) ? "error" : "ok"; } } } }