Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php exec mysql empty output

Tags:

php

mysql

exec

I try to capture the error output.

   <?php
        $output = array();
        $command = <<<END
mysql -h$host -u$user --password='$pass' --execute="create database $name;" 2>&1
END;
        exec($command, $output, $code);
    ?>
  1. $output returns no value
  2. $code returns 0

But this query returns an error in the terminal: "database already exists".

When I remove 2>&1

     $command = <<<END
        mysql -h$host -u$user --password='$pass' --execute="create database $name;"
END;
     exec($command, $output, $code);
  1. $output returns no value
  2. $code returns 1

How can I get the correct $output and $code value?

like image 725
Mooncat No Avatar asked Feb 12 '26 00:02

Mooncat No


1 Answers

Had the same problem, except that I was importing a .sql file instead of using --execute

It worked after I added 2>&1 and using double quotes instead of singles.

exec("mysql -h $host -u $user -p$pass my_db < \"faulty_sql.sql\" 2>&1",$output);
like image 153
Paul Salameh Avatar answered Feb 14 '26 14:02

Paul Salameh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!