2016-12-27 95 views
0

我試圖插入一些數據到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" 

任何幫助,將不勝感激。 在此先感謝。

+0

很可能是因爲您傳遞到命令的參數是轉義 –

+0

使用,因爲它們是在depricated mysql_ *功能不建議PHP 5.5.0,並將在PHP 7.0.0中被刪除。改用mysqli_ *函數。 http://php.net/manual/en/function.mysql-query.php –

+0

嗨@SumanBanerjee,問題在於exec()函數,而不是在mysql_ *函數中。謝謝。 –

回答

0

「它不工作」的含義是什麼? 它是否做空記錄或什麼都沒有?

根據exec quickref

你可以通過指針,以獲得命令的輸出和狀態。

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

嗨@Pythagoras,感謝您的輸入。它什麼也沒做。正如你所看到的,我試圖向SQLAnywhere數據庫中插入1個數據,我已經從.php文件中提取了查詢的返回字符串並將其複製到cmd.exe,並且它運行良好,並按照我的預期插入。事情是,當我嘗試運行它時,exec()命令什麼也沒做。 –

+0

@sunstation您可以通過以下語法獲得有關錯誤的更多信息:exec('some_command 2>&1',$ output);的print_r($輸出)。它會告訴你什麼不是。 – Pythagoras

+0

是的,錯誤是關於odbc連接,好像它不能連接到odbc,但是命令在cmd.exe中完美工作! –

0

既然你直接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"';

相關問題