2

I M Using YII to develop web application

i want to check if query executed successfully or not

$data = Yii::app()->db->createCommand($SQL);
$result = $data->queryAll();

if(count($result) == 0)
{
    throw new SoapFault('Sender', 'Unable to get display information.');
}

if code will execute if select query return no result-set. but i want to check if query executed successfully or not. based on that i want to throw exception. then if query executed successfully and returned no result-set then some other exception.

how to do this ? any suggestions ?

2
  • Yii will throw an exception by itself if the query fails due to syntax errors or other problems. Commented Mar 25, 2014 at 6:45
  • for service to execute properly i need to throw error explicitly... Commented Mar 25, 2014 at 7:05

2 Answers 2

5
try {
    $result = $data->queryAll();
} catch (Exception $ex) {
    echo 'Query failed', $ex->getMessage();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I made a DBCommand with a param and wrongfully added quotes: UPDATE ... WHERE hash = ':hash', the command didn't work but didn't throw an exception either. What should one do in such a case?
If a query has syntax error or failed to execute on database it throws exception. What do you mean by command didn't work? If you have logging setup-ed you can debug SQL generated by yii (log file in runtime dir).
What I mean is that the update doesn't take place. The query per-se has no error, but as Yii adds its own quotes when replacing a string parameter, I guess the quotes I added in the query conflict with that, but the query does not throw an SQL error because the query never gets ran actually.
Well this is fine i think, can you post the code so I can see what is going wrong? Alternatively you can throw your own exception if no rows effected!
0
try 
{
   $result = Yii::app()->db->createCommand($sqlQuery)->execute();
   echo  'success';
}
catch (Exception $e) 
{
   echo  'fail';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.