0

Im using php exec() to import an uploaded sql file.

$cmd = "mysql -h localhost -u forum_user --password=1234 import_forum < C:\Users\Josh\AppData\Local\Temp\import-1452620686.sql"
exec($cmd, $output, $worked);
return $worked;

The problem is, its not actually executing anything because nothing is imported to the database. However, if I copy and paste this command into the terminal, it imports just fine.

This seems to be an issue locally on my windows machine because the script works fine in our linux environment.

Also, the $worked variable is returning as 1, which should mean that the command is executed without a problem. But that is not the case.

Is this some kind of a permissions issue? What am I missing?

Edit 1

The mysql path is set and working properly.

Edit 2

Actual path is C:\Users\Josh\AppData\Local\Temp\import-1452620686.sql

Edit 3

$output is an empty array

16
  • 1
    Is This mysql -h localhost -u forum_user --password=1234 import_forum < C:\path\to\location.sql run from cmd? Commented Jan 12, 2016 at 17:42
  • 1
    Don't know the real path, but \t is the tab escape sequence for example, so I'm guessing your Windows path separators are not being escaped. Commented Jan 12, 2016 at 17:44
  • 1
    Try to run mysql -h localhost -u forum_user --password=1234 import_forum < C:\path\to\location.sql from CMD(command prompt) Commented Jan 12, 2016 at 17:45
  • 1
    which version of xampp, wamp or php are you using? Commented Jan 12, 2016 at 17:47
  • 1
    In the file path, try doubling each of the backslashes eg. C:\\Users\\path\\to\\file.sql Commented Jan 12, 2016 at 17:49

2 Answers 2

2

I couldn't really find a definitive answer to this on stackoverflow so here is all the information I have cobbled together from various sources to get this to finally work, hope it saves somebody some time. Here is some code to perform a mysql import using php exec and passing a varaiable for the sql import file name. This is running under windows 7 on a WAMP server. Note the back slashes for the path to mysql (paths will differ for different versions of WAMP) and the forward slashes for the path to the sql import file. Also the parenthesis around the command inside the quotes to get the redirect of the message output. And finally the redirect of the output "2>&1".


    $sqlFile='sqlfile.sql';
    $cmd="(C:\wamp64\bin\mysql\mysql5.7.14\bin\mysql --user=USER --password=PASSWORD --host=localhost --database db1 db2 < C:/wamp64/www/site/$sqlFile) 2>&1";
    exec($cmd, $output, $return_var);

    var_dump($return_var);
    echo "return_var is: $return_var" . "\n";
    var_dump($output);
Sign up to request clarification or add additional context in comments.

Comments

0

For some reason, adding C:\xampp\mysql\bin\mysql to the beginning of the command resolved my issue.

I dont understand why this is the case, since I added C:\xampp\mysql\bin to the path system variable. When I use the command line manually, I can use the mysql command fine, but PHP appears to need the location..?

Comments

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.