shell_exec not returning any errors?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zany_cynic8
Forum Newbie
Posts: 4
Joined: Tue Jul 18, 2006 3:18 am

shell_exec not returning any errors?

Post by zany_cynic8 »

Hi everyone..I'm fairly new to PHP and I'm stuck with this problem..I have this snippet of code that won't work as expected

Code: Select all

$db_host = "localhost";
$db_port = "3306";
$db_name = "myDatabase";
$db_dump = "db.sql"
$result = shell_exec("mysqldump --host=$db_host --port=$db_port --opt $db_name > $db_dump &2>1");
This does work however when I pass in a false $db_name it should generate an error but it doesn't
It seems like shell_exec is not getting the error message from mysql

I have tried to e.g.
echo shell_exec("error command");
and is giving me an error..

what's wrong with shell_exec not retrieving error message when used mysqldump command??
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

As far as I am aware shell_exec only returns output that goes to STDOUT. mysqldump is probably returning it's error to STDERR.
zany_cynic8
Forum Newbie
Posts: 4
Joined: Tue Jul 18, 2006 3:18 am

Post by zany_cynic8 »

add '2>&1' to the end of your shell command to have STDERR returned as well as STDOUT.
from http://au2.php.net/manual/en/function.s ... .php#28994

any other suggestions on how I can get the error messages? thanks
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

You are redirecting all output to $db_dump. Your error is probably being redirected there aswell because you have redirected STDERR to STDOUT.
zany_cynic8
Forum Newbie
Posts: 4
Joined: Tue Jul 18, 2006 3:18 am

Post by zany_cynic8 »

I'm not getting it.. Do you have any ideas on how I could get the error message form mysql? please please help..thanks
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Don't redirect STDERR to STDOUT and then read the latest comment at http://php.net/shell_exec

You want to tweak that function to return $stderr
zany_cynic8
Forum Newbie
Posts: 4
Joined: Tue Jul 18, 2006 3:18 am

Post by zany_cynic8 »

thanks for the help..I followed your advice and now it's working fine...
Post Reply