【问题标题】:Yii foreach error?Yii foreach 错误?
【发布时间】:2026-01-06 01:30:01
【问题描述】:

我调用了一个函数。获取表号 (result=0) 结果并将相同的表值更新为 0 到 1。我正在使用更新查询。我已运行此函数以返回错误 :: Missing argument 2 for CDbCommand::update()。

 public function newdisplaycontent()
{
    $count = Yii::app()->db->createCommand()
        ->select()
        ->from('scrolltable')
        ->where('result=:result', array(':result'=>0))
        ->queryAll();
$rs=array();
//print_r($count);

foreach($count as $item){
//process each item here
    $rs=$item['ID'];
    $user=Yii::app()->db->createCommand()
    ->update("scrolltable SET result = 1")
    ->where('ID=:id', array(':id'=>$rs));
}

return $rs;
}

感谢您的功能帮助..

【问题讨论】:

    标签: yii yii-extensions


    【解决方案1】:

    update() 的正确语法如下:

    $user=Yii::app()->db->createCommand()
    ->update("scrolltable",array("result" => "1"))
    ->where('ID=:id', array(':id'=>$rs));
    

    作为官方文档:

    update() 创建并执行 UPDATE SQL 语句。该方法将正确地转义列名并绑定要更新的值。

    public integer update(string $table, array $columns, mixed $conditions='', array $params=array ( ))
    

    【讨论】: