pdo - 'PDOException' with message 'SQLSTATE[07002]: [Microsoft][SQL Server Native Client 11.0]COUNT field incorrect or syntax error' in .../db.php:46

432

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 {

    }

}
355

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:

for ($i = 1; $i <= count($args); $i++){
          $stmt->bindParam($i, $args[$i - 1 ]["value"], $args[$i - 1]["type"]); 
}

Hope this helps. It worked for me. Good luck.

People are also looking for solutions to the problem: ANSI <--> UTF-8 charset issue, when displaying images from the database (PHP)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.