开发者

how to correct run mysql command using php system() function?

I can't find any question exactly like this. If I'm wrong please point me to the right answer about this.

I have the following situation:

I need to run a script in the cron job (no problem), the problem is in my script, something is wrong, 'cause the script is not running properly.

NOT WORKING SCRIPT

<?php

$command = 'mysql -u root -h localhost eephi < C:/wamp/w开发者_StackOverflow社区ww/file-20110817-0200.sql';
system( $command );

?>

The most weird is, the command -> mysql -u root -h localhost eephi < C:/wamp/www/file-20110817-0200.sql when executed is a shell, works perfect, no errors, everything run fine. But when I call the command in a php script like is showed above it is not working, no errors, no warnings, nothing is outputed.

I have another script doing something similar, but not the same, but the idea is the same.. and it works fine.

WORKING SCRIPT

$command = 'mysqldump -u user -p****** -h myhost.com mytable > /home/user/www/backup/file-20110817-0200.sql';
system( $command );

?>

Then what is the difference between the two scripts? one works fine and another one not.. WEIRD to me. I hope I can find a solution here. Thanks anyway for your time.


Probably a path error - executing things from within PHP has a different environment than from the command line. You'll probably have to provide a full path to mysql in your exec (e.g. /usr/local/bin/mysql ...)

You can capture the shell's error messages by appending 2>&1 to the end of the command. This'll redirect stderr to stdout so PHP sees the error messages as part of the command's output.


Have you tried using

exec(); 

instead of

system();


Maybe the difference is not in the scripts, but in the running environment? Have you tried running the second script on your localhost?


I can't find option -p in 'NOT WORKING SCRIPT'. So if you run sql command from shell under root it's not necessary to input password and all work fine. It seems for php it doesn't work or you run php under another user.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜