3

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?

2
  • Any reason for not using mysql_create_db()? Commented Nov 4, 2011 at 9:58
  • Unfortunately it doesn't suit me. I write my own t_exec() function which executes and captures errors for shell commands. Commented Nov 4, 2011 at 10:31

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

Use system()

p.s: why don't use msyql_query('your query'); ?

2 Comments

I write my own exec() function which executes and captures errors for shell commands.
system returns an empty result too

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.