我在 windows apache 环境中使用 PHP 来使用 pdo 连接 sql server。我的代码运行完美,没有任何错误。Windows 机器上的最新更新使 pdo 插入查询不起作用。为了确认这个错误不是因为最近的变化,我已经从 SVN 恢复到旧版本。仍然存在相同的错误。
错误是
INSERT INTO contactus(contactref, title, first_name, last_name, email) VALUES('35008679022', 'Mr', 'Robin', 'Michael', 'robin@robin.com')
上述查询返回错误
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 )
但是,当我复制查询并在 SQL Server Management Studio 中运行它时,它运行时没有任何错误。
谁能帮我找出这个问题?
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;
}
导致错误的实际功能
$flds = 'contactref, title, first_name, last_name, email';
$prepVals = '?, ?, ?, ?, ?';
$valsArr = array('35008679022', 'Mr', 'Robin', 'Michael', 'robin@robin.com');
newInsertQuery('contactus', $flds, $prepVals, $valsArr);