使用PHP exec调用MySQL exe不起作用 [英] Calling MySQL exe using PHP exec doesn't work
问题描述
我有一个使用PHP exec
的极其简单的脚本,调用了mysql
命令:
I have an extremely simple script with PHP exec
, calling mysql
command:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'mypass';
$db = 'job';
$file ='job_create.sql';
$mySQLDir='"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql"';
if ($dbpass != '') {
$cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' < "'.dirname(__FILE__).'\\'.$file.'"';
} else {
$cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' < "'.dirname(__FILE__).'\\'.$file.'"';
}
// echo $cmd;
exec($cmd,$out,$retval);
我希望上面的脚本调用mysql
命令,传递用户身份验证信息并即时运行job_create.sql
.
I would expect that the above script calls mysql
command, pass in the user authentication information and run the job_create.sql
on the fly.
从job_create.sq
l不能正常运行的意义上说,它只是不起作用. .我尝试使用以下脚本直接从命令行调用mysql
命令
The only thing is that it doesn't work, in the sense that the job_create.sq
l is not run properly. . I tried to call mysql
command directly from command line using the below script,
bin\mysql.exe -h localhost --user=root --password=mypass < "job_create.sql"
,并且有效.
有什么办法解决这个问题吗?
Any idea how to fix this?
我从PHP命令行调用此脚本.即,PHP.exe installdb.php
推荐答案
我找到了解决方法
问题是您需要将$cmd
明确包含在"中,即
The problem is that you need to explicitly enclosed the $cmd
in "".i.e.,
exec('"'.$cmd.'"',$out ,$retval);
这是有效的完整代码:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$db = 'job';
$file =dirname(__FILE__).'\\'.'job_create.sql';
$mySQLDir='"C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe"';
if ($dbpass != '') {
$cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' < "'.$file.'"';
} else {
$cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' < "'.$file.'"';
}
echo $cmd;
exec('"'.$cmd.'"',$out ,$retval);
echo "\n";
echo ($retval);
?>
这篇关于使用PHP exec调用MySQL exe不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!