Reputation: 375
I didn't want to post this but last resort.
PHP with MS SQL Server 2008 and I'm getting an error of: [SQLSTATE[07002]: [Microsoft][ODBC Driver 11 for SQL Server]COUNT field incorrect or syntax error Here is my code:
$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));
The array shows:
print_r(array($sqlVals));
Array ( [0] => '1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21' )
when I try this, the insert is successful without error:
$stmt->execute(array('1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21'));
Not sure how to correct - any help is much appreciated!
Upvotes: 0
Views: 2653
Reputation: 78
In First example $sqlVals is treated as 1 string value not distinct values
so you can try this
$sqlVals = array('1', '233', '34', 'Delivery', 'Data Integrity', 'Good', '2018-07-11 08:34:21');
$stmt->execute($sqlVals);
Upvotes: 2