pdo - 'PDOException' with message 'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error' in .../db.php:46
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error' in .../db.php:46 Stack trace: #0 .../db.php(46): PDOStatement->execute() #1 .../tests/testDb.php(12): ReportsDB->runSP('CALL ...', Array) #2 {main} thrown in .../db.php on line 46
I am getting this error on some code I am working on. I am having problems debugging this particular error message.
Here's what's encapsulating line 46 (I'll highlight the actual line).
public function runSP ($sp, array $args) {
$returnParams = array();
if ($sp) {
$s = explode(" ", $sp);
if ($s[0] != "CALL") {
$sp = "CALL " . $sp;
}
$stmt = $this->_db->prepare($sp);
if (sizeof($args) > 0) {
for ($i = 0; $i > sizeof($args); $i++) {
$stmt->bindParam($i, $args[$i]["value"], $args[$i]["type"]);
$t = explode("|", $args[$i]["type"]);
if ($t[1] == "PDO::PARAM_INPUT_OUTPUT") {
$returnParams[] = $args[$i]["value"];
}
}
Line 46 $stmt->execute();
return $returnParams;
} else {
}
}
Answer
Solution:
I've done this earlier and also had the same error. Here's how I solve mine.
1.) Take a look at your query. Make sure you're not calling column names that doesn't really exist on your table.
2.) The number of column names and the number of placeholders must match.
3.) In your for loop, instead of initializing the counter($i) to 0, initialize it to 1. That way your saying that on the first iteration of the loop, you are binding the first value of the array ($args[$i-1]["value"]) to the first placeholder on your query. So it should be:
Hope this helps. It worked for me. Good luck.