1

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/www/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.

1
  • Why aren't you just using php to import the file? Why use the system() command? Commented Aug 17, 2011 at 16:53

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have two server one is linux and the another is windows, the problem is with the windows server, follow your advice I just need to call the full path for my mysql server, yeah? something like this -> C:\wamp\bin\mysql\mysql5.1.36\bin\mysql.exe instead of mysql ?? thanks.
Yeah. but note that a full windows path will of course not work on th elinux server. You should probably use a var to store a per-server path and insert the var's contents into the command line.
@That's right, the path, also I will use a var to store a per-server. Work perfect. The problem was solved. Thank You very much.
0

Have you tried using

exec(); 

instead of

system();

2 Comments

yes I did. and not success at all. The same results, or no results.
Thank You for your effort. The problem was solved, it was a problem in the path.
0

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

2 Comments

perheps. make sense, I will try.
Thank You for your effort. The problem was solved, it was a problem in the path.
0

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.

1 Comment

my user is root without a password. Of course only in localhost. Then I do not need to specify the -p option because it causes to prompt for a password, even if is a blank password. But the problem was solved, was only the path. Thanks for your effort.

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.