1
In PDO today was the first time I had to put a variable in LIMIT, and I’m getting this error:
Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[HY093]: Invalid Parameter number: number of bound variables does not match number of tokens' in filename.php:85 Stack trace: #0 name-doarquivo.php(85): Pdostatement->execute(Array) #1 {main} thrown in name-doarquivo.php on line 85
The line 85 is this:
$sql2->execute(array(":idQuiz" => $idQuiz));
My code:
$perguntaQuiz = 2;
$sql2 = $pdo->prepare('SELECT * FROM quiz_pergunta WHERE idquiz = :idQuiz ORDER BY id ASC LIMIT 0, :perguntaQuiz');
$sql2->bindParam(':perguntaQuiz', $perguntaQuiz, PDO::PARAM_INT);
$sql2->execute(array(":idQuiz" => $idQuiz));
What am I missing?
The
(int)
is not necessary.– stderr
Now gave another error... vo edit
– caiocafardo
Just like @zekk said, remove the
cast
. If you really need it, which in case doesn’t seem necessary, do it previously outside thebindParam
.– Marcelo de Andrade
I don’t get it @Marcelodeandrade
– caiocafardo
In your code exemplified in the question, there is no need for cast
(int)
since php will already interpret the value as an integer when you did$perguntaQuiz = 2;
– Marcelo de Andrade
I got it, I changed it, but there was another mistake.... I edit the question!
– caiocafardo
Duplicate of Number of "bound variables" does not match the number of "tokens" ?
– Bacco
you left the same code as the answers?
– rray