PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 30, 2022

[FIXED] CDbCommand failed to execute the SQL statement.?

 January 30, 2022     mysql, php, yii     No comments   

Issue

I have the following update code in my controller. The sql query is executing correctly and I can see the updated result in backend. But while executing its showing error, 'CDbCommand failed to execute the SQL statement'. Is there any other way to write this statement. Contoller codes are as follow.

public function actionactiveuser()
 {          

 $user_id=$_GET['id'];
 $query = "UPDATE user SET status = '1' WHERE id = '$user_id'";
 Yii::app()->db->createCommand($query)->queryAll();
 $this->render('login');    
 }

It is showing error:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error. The SQL statement executed was: UPDATE user SET status = '1' WHERE id = '80'

Solution

change this

Yii::app()->db->createCommand($query)->queryAll();

to

Yii::app()->db->createCommand($query)->execute();

queryAll() is used for select queries. execute() is for Update and Insert queries

BTW For the safety you should do something like this

$query = "UPDATE user SET status = :status' WHERE id = :id";
 Yii::app()->db->createCommand($query)->execute(array(':status' => '1',':id'=>$user_id ));


Answered By - Let me see
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing