Php 使用命令Create not working更新Yii中的查询
我正在尝试从Yii框架更新表,但出现错误 我的代码如下:Php 使用命令Create not working更新Yii中的查询,php,mysql,sql,yii,Php,Mysql,Sql,Yii,我正在尝试从Yii框架更新表,但出现错误 我的代码如下: $updatebrick = Yii::app()->db->createCommand("update link_bricks SET parent_id=". $v[parent_id] ." where child_id=". $k[micro_brick_id] ." ORDER BY id ASC LIMIT 1")->queryAll(); 错误: CDbCommand failed to execute
$updatebrick = Yii::app()->db->createCommand("update link_bricks SET parent_id=". $v[parent_id] ." where child_id=". $k[micro_brick_id] ." ORDER BY id ASC LIMIT 1")->queryAll();
错误:
CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error. The SQL statement executed was: update link_bricks SET parent_id=1963 where child_id=15793 ORDER BY id ASC LIMIT 1
您应该使用
execute
功能,而不是queryAll
queryAll
用于SQL(SELECT
语句),execute
用于DML(UPDATE
,INSERT
,DELETE
语句)。
看
您还应该考虑使用框架来绑定参数,而不是注入它们(出于安全和理智的原因)。 我建议:
$sql='
UPDATE link_bricks
SET parent_id=:parent_id
WHERE child_id=:child_id
ORDER BY id ASC
LIMIT 1
';
$command=Yii::app()->db->createCommand($sql);
$update=$command->execute(array(
'parent_id'=>$v[parent_id],
'child_id'=>$k[child_id],
);
@ramamoorthy_villi ORDER BY和LIMIT在更新中是允许的,请看它在直接从phpMyAdmins而不是->queryal()执行时工作良好;使用->查询()@拉马穆尔蒂·维利·诺普;使用
执行
@Himanshu没问题,乐意帮忙。如果你能接受这个答案,那就太好了!哦,对不起:)我也会投票,但在这里没有那么大的声誉