How to get errors from executing an SQL script?

Hi All,

I am executing an SQL script this way:

public function actionMigrate(){

   $connection = Yii::app()->db;

   $sql = file_get_contents('migration_script.sql');

   $command = $connection->createCommand($sql)->execute();

If I run the file in sql console it shows the erros (if it has any), but if I run the action above it does not show the errors…

How do I show the errors?

Have you tried to try-catch them?

Yes like this:

public function actionMigrate(){

   $connection = Yii::app()->db;


      $sql = file_get_contents('migration_script.sql');

      $command = $connection->createCommand($sql)->execute();


   catch (Exception $e){

      throw new CDbException('Test');


Still it does not show the error…

What do you see with

catch (Exception $e) {




I don’t see anything with var_dump…

The action runs as if it doesn’t catch any error at all…

So… are you sure there are errors?

Well, if I run the sql query in phpmyadmin I get the error…but if I run that action containing this query, it does not show any error…

Ok, so tell us what shows when you add


before $command.

it shows the whole script…

Well, since I don’t have much time for this ping-pong:

  • How many queries are inside the file?

  • If more than one - can you do that? I have never tried it before.

  • I don’t know what error you got from phpmyadmin so cannot help with that.

Thank you Bizley for your time.

You were right, I tried with a single query and it works (it shows the error)…but the file has a lot of queries…

I can not do try-catch for each query when I have more than 100 queries!

Does anyone knows how to catch the error from a group of queries?

Do you need something that allows to execute queries from the file? I mean like more than once?

Google show something like this