Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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?

share|improve this question
    
Any reason for not using mysql_create_db()‌​? – manatwork Nov 4 '11 at 9:58
    
Unfortunately it doesn't suit me. I write my own t_exec() function which executes and captures errors for shell commands. – Mooncat No Nov 4 '11 at 10:31

2 Answers 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);
share|improve this answer

Use system()

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

share|improve this answer
    
I write my own exec() function which executes and captures errors for shell commands. – Mooncat No Nov 4 '11 at 10:04
    
system returns an empty result too – Mooncat No Nov 4 '11 at 10:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.