0

I am using PHP in windows apache environment for connecting sql server using pdo. My code was running perfectly with out any errors. recent update on windows machine making pdo insert query not working. To confirm this error is not because of recent changes, I have reverted to older versions from SVN. Still same error exists.

error is

INSERT INTO contactus(contactref, title, first_name, last_name, email) VALUES('35008679022', 'Mr', 'Robin', 'Michael', 'robin@robin.com')

The above query returns an error

Array ( [0] => HY000 [1] => 10007 [2] => Incorrect syntax near '35008679022[10007] (severity 5) [INSERT INTO contactus(contactref, title, first_name, last_name, email) VALUES('Mr] [3] => -1 [4] => 5 )

But, when i copy the query and run it in SQL server management studio, It runs without any error.

Can anyone please help me to find out this issue?

function newInsertQuery( $tbl, $flds, $prepVals, $valsArr ) {
    try {
        $dbcon = $this->mysqlConnect();
        $q = "INSERT INTO $tbl($flds) VALUES($prepVals)";
        $prep = $dbcon->prepare($q);
        if( $prep->execute($valsArr) ) {
            $rq = 1;
            $q = "SELECT SCOPE_IDENTITY() as ins_id";
            $rq = $dbcon->query($q);
            $optArr = $rq->fetchAll(PDO::FETCH_ASSOC);
            $_SESSION['last_id'] = $optArr[0]['ins_id'];
        }
        else {
            $rq = 0;
        }

        $_SESSION['q_error'] = $dbcon->errorInfo();
    }
    catch( Exception $ex ) {
        $rq = 0;
        echo $ex->getMessage();
    }

    //clear connection
    $dbcon = null;
    return $rq;
}

Actual function that's causing an error

$flds = 'contactref, title, first_name, last_name, email'; 
$prepVals = '?, ?, ?, ?, ?'; 
$valsArr = array('35008679022', 'Mr', 'Robin', 'Michael', 'robin@robin.com');
newInsertQuery('contactus', $flds, $prepVals, $valsArr);
default locale
  • 13,035
  • 13
  • 56
  • 62
Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36
  • How are you issuing the SQL statement ? http://stackoverflow.com/questions/4599604/escapeing-values-in-pdo-statements – Marcello Romani Jun 25 '13 at 06:05
  • 1
    Possible duplicate of http://stackoverflow.com/questions/17270935/php-pdo-mssql-error?rq=1 – Marcello Romani Jun 25 '13 at 06:08
  • @MarcelloRomani Thanks for reply. I am preparing and executing values with array. – Robin Michael Poothurai Jun 25 '13 at 06:10
  • @MarcelloRomani function newInsertQuery( $tbl, $flds, $prepVals, $valsArr ) {try {$dbcon = $this->mysqlConnect(); $q = "INSERT INTO $tbl($flds) VALUES($prepVals)";$prep = $dbcon->prepare($q); if( $prep->execute($valsArr) ) {$rq = 1;$q = "SELECT SCOPE_IDENTITY() as ins_id";$rq = $dbcon->query($q);$optArr = $rq->fetchAll(PDO::FETCH_ASSOC);$_SESSION['last_id'] = $optArr[0]['ins_id'];}else {$rq = 0;}$_SESSION['q_error'] = $dbcon->errorInfo();}catch( Exception $ex ) {$rq = 0;echo $ex->getMessage(); }//clear connection $dbcon = null; return $rq; } – Robin Michael Poothurai Jun 25 '13 at 06:57
  • And what's the _actual_ function call that's causing the error ? – Marcello Romani Jun 25 '13 at 07:08
  • @MarcelloRomani $flds = 'contactref, title, first_name, last_name, email'; $prepVals = '?, ?, ?, ?, ?'; $valsArr = array('35008679022', 'Mr', 'Robin', 'Michael', 'robin@robin.com'); newInsertQuery('contactus', $flds, $prepVals, $valsArr); – Robin Michael Poothurai Jun 25 '13 at 07:12
  • Works for me. That is, the test data is inserted into the db table. Are you sure `mysqlConnect()` works ? Also, have you tried putting `ini_set('display_errors',1);` at the beginning of the page ? – Marcello Romani Jun 25 '13 at 07:51
  • @MarcelloRomani no no.. this is MSSQL, and finally comes to end of reasearch, I have confirmed that happened bcz of windows update. I have changed mssql pdo driver to sqlsrv pdo driver, that worked, Thanks a lot for your co-operation – Robin Michael Poothurai Jun 25 '13 at 07:57
  • Oh, sorry! :-P Thought it was a problem in the code _after_ the connection. Anyway, glad you eventually sorted it out. – Marcello Romani Jun 25 '13 at 08:10

1 Answers1

1

Finally, I have found the issue, This is because of recent windows update on my machine and our windows server. After this update mssql pdo driver throws this error. I have downloaded sqlsrv20 driver from microsoft website and placed the file
php_pdo_sqlsrv_53_ts_vc6.dll inside php extension directory and added new extension in php.ini file like extension=php_pdo_sqlsrv_53_ts_vc6.dll and changed the pdo connectivity

$dbcon = new PDO( "sqlsrv:server=$conf->host;Database=$conf->db_name", $conf->db_user, $conf->db_pwd); 

Note : for wamp server users needs to check php.ini in apache also and needs to add the extension in php.ini file under apache.

Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36