问题
I need to run Shell_exec to import a dump into my MYSQL database. Today I do this:
$shellExec = 'C:\wamp\bin\mysql\mysql5.1.36\bin\mysql -h localhost -u root mertero_decicare < D:\localhost\sql\decisions.sql';
$out = shell_exec($shellExec .' 2> output');
Which works. But is there a way to 'find' the location of the mysql executable from PHP? SO I don't need to hardcaord my mysql location?
Ron
回答1:
An easy way to do this is add it to the Windows Environment path (it is in the path on most Unix systems). Then you just have to call mysql from the script.
Here is the information page on how to do it from MySQL: MySQL Windows Service Start
And on that page you want to find the text
"To make it easier to invoke MySQL programs, you can add the path name of the MySQL bin directory to your Windows system PATH environment variable"
and read through that section. Using this method you just have to call mysql without a path.
回答2:
On Windows to could add C:\wamp\bin\mysql\mysql5.1.36\bin\ to you Environment Variables path. See: http://en.wikipedia.org/wiki/Environment_variable
That way you could just call shell_exec('mysql ...');
Alernatively you could not use mysqldump, you could use SELECT INTO OUTFILE in MySQL although you'd need to do it for each table. Not as elegant as mysqldump.
来源:https://stackoverflow.com/questions/3309094/can-i-automatically-locate-the-mysql-executable-from-php