Himanshu
Himanshu

Reputation: 148

Update Query in Yii using Command Create not working

I am trying to update a table from Yii Framework, but getting error

My code is as below:

$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();

Error:

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 

Upvotes: 1

Views: 1968

Answers (1)

Arth
Arth

Reputation: 13110

You should use the execute function instead of queryAll. queryAll is for SQL (SELECT statements) and execute is for DML (UPDATE,INSERT,DELETE statements). See docs.

You should also consider binding the parameters in using the framework, rather than injecting them (for security and sanity reasons).

I would suggest:

$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],
);

Upvotes: 3

Related Questions