简体   繁体   中英

PHP Exec not working but the command itself is working fine

I tried to insert some data to 2 separate db platforms, first one is mySql and the other one is SQLAnywhere 11. For mysql i used the php mysql_query method to insert the database, and for SQLAnywhere 11 i'm using the dbisql command with php exec() function. The command itself is working properly in the cmd.exe but when i tried it with php it's not working.

Here's the trial code :

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

    }

?>

Been trying to run this script some couple times and it always works.

Example:

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"

Any help would be appreciated. Thanks in advance.

What's the meaning of "it's not working"? Does it do an empty record or nothing?

According the exec quickref

you can pass pointers in to get the output and status of the command.

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

Since you are concatinating your query directly to the command the output will be like this:

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"

on command prompt this will be an error because:

insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2')

have spaces and special characters

try using "insert into table_person (cid, first_name, last_name) values (70, 'test', 'test 2')"

so your command will be:

$cmd = 'C:/"Program Files"/"SQL Anywhere 11"/Bin32/dbisql.exe -d1 "'.strtolower($query2).'" -c "UID=dba;PWD=axia1234;DSN=Payroll11"';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM