PHP执行不工作,但命令本身工作正常
我试图插入一些数据到2个独立的数据库平台,第一个是mySql,另一个是SQLAnywhere 11.对于MySQL我用php mysql_query
方法插入数据库,而对于SQLAnywhere 11,我使用dbisql
命令和php exec()
函数。该命令本身在cmd.exe
中正常工作,但是当我尝试使用php时,它不起作用。PHP执行不工作,但命令本身工作正常
这里的审判代码:
<?php
$cid = rand(0, 100);
$first_name = "Test";
$last_name = "Test 2";
$connect = mysql_connect("localhost", "root", "");
if ($connect) {
mysql_select_db("db_person");
$query = mysql_query("INSERT INTO table_person (cid, first_name, last_name) VALUES ($cid, '$first_name', '$last_name')") or die(mysql_error());
$query2 = "INSERT INTO table_person (cid, first_name, last_name) VALUES ($cid, '$first_name', '$last_name')";
$cmd = 'C:/"Program Files"/"SQL Anywhere 11"/Bin32/dbisql.exe -d1 '.strtolower($query2).' -c "UID=dba;PWD=axia1234;DSN=Payroll11"';
$output = NULL;
$exec = exec($cmd, $output);
var_dump($output);
} else {
echo "Fail";
}
?>
一直试图运行此脚本一些几次,它总是有效。
实施例:
C:/"Program Files"/"SQL Anywhere 11"/Bin32/dbisql.exe -d1 insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2') -c "UID=dba;PWD=axia1234;DSN=Payroll11"
任何帮助,将不胜感激。 在此先感谢。
“它不工作”的含义是什么? 它是否做空记录或什么都没有?
你可以通过指针,以获得命令的输出和状态。
$exec = exec($cmd, $output, $pointer);
if(!pointer){
echo "success exec\n";
}
else{
echo "failed exec \n";
}
嗨@Pythagoras,感谢您的输入。它什么也没做。正如你所看到的,我试图向SQLAnywhere数据库中插入1个数据,我已经从.php文件中提取了查询的返回字符串并将其复制到cmd.exe,并且它运行良好,并按照我的预期插入。事情是,当我尝试运行它时,exec()命令什么也没做。 –
@sunstation您可以通过以下语法获得有关错误的更多信息:exec('some_command 2>&1',$ output);的print_r($输出)。它会告诉你什么不是。 – Pythagoras
是的,错误是关于odbc连接,好像它不能连接到odbc,但是命令在cmd.exe中完美工作! –
既然你直接concatinating查询到该命令的输出将是这样的:
C:/"Program Files"/"SQL Anywhere 11"/Bin32/dbisql.exe -d1 insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2') -c "UID=dba;PWD=axia1234;DSN=Payroll11"
在命令提示符下,这将是一个错误,因为:
insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2')
有空格和特殊字符
尝试使用"insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2')"
所以你的命令是:
$cmd = 'C:/"Program Files"/"SQL Anywhere 11"/Bin32/dbisql.exe -d1 "'.strtolower($query2).'" -c "UID=dba;PWD=axia1234;DSN=Payroll11"';
很可能是因为您传递到命令的参数是转义 –
使用,因为它们是在depricated mysql_ *功能不建议PHP 5.5.0,并将在PHP 7.0.0中被删除。改用mysqli_ *函数。 http://php.net/manual/en/function.mysql-query.php –
嗨@SumanBanerjee,问题在于exec()函数,而不是在mysql_ *函数中。谢谢。 –