簡體   English   中英

PHP Exec無法正常運行,但命令本身可以正常運行

[英]PHP Exec not working but the command itself is working fine

我嘗試將一些數據插入2個單獨的數據庫平台,第一個是mySql,另一個是SQLAnywhere11。對於mysql,我使用php mysql_query方法插入數據庫,對於SQLAnywhere 11,我將dbisql命令與php一起使用exec()函數。 該命令本身在cmd.exe正常工作,但是當我嘗試使用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 quickref

您可以傳遞指針以獲取命令的輸出和狀態。

 $exec = exec($cmd, $output, $pointer);
 if(!pointer){
   echo "success exec\n"; 
 }
 else{
 echo "failed exec \n";     
 } 

由於您要將查詢直接附加到命令中,因此輸出將如下所示:

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"';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM