我不想发布此消息,但不得已。
带有MS SQL Server 2008的PHP,出现以下错误:[SQLSTATE [07002]:[Microsoft] [SQL Server的ODBC驱动程序11] COUNT字段不正确或语法错误 这是我的代码:
$server_name = 'sqlserver';
$user = 'user';
$passwd = 'pass';
$sqlString = "INSERT INTO dbo.tbl (UniqID, rideid, stopRatingId, category, ddescription, rating, createdDate) VALUES (?, ?, ?, ?, ?, ?, ?)";
$conn = new PDO("sqlsrv:Server=$server_name;Database=db;ConnectionPooling=0", $user, $passwd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($sqlString);
$sqlVals = "'1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21'";
$stmt->execute(array($sqlVals));
该数组显示:
print_r(array($sqlVals));
Array ( [0] => '1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21' )
当我尝试此操作时,插入成功且没有错误:
$stmt->execute(array('1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21'));
不确定如何纠正-非常感谢您的帮助!
答案 0 :(得分:2)
在第一个示例中,$ sqlVals被视为1个字符串值而不是不同的值
所以您可以尝试
$sqlVals = array('1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21');
$stmt->execute($sqlVals);