PDO - Invalid parameter number

Most recently, I switched to using PDO in PHP / MySQL and converted a few dozen queries. Most of them worked, however this very simple one throws an exception in$sql->execute()

$sql=$pdo->prepare("SELECT id FROM user WHERE username = :username LIMIT 1");
$sql->execute(array(':username',$username));

PDOStatement :: execute () pdostatement.execute SQLSTATE [HY093]: Invalid parameter number: the number of related variables does not match the number of tokens in ...

After researching, I found this link: https://bugs.php.net/bug.php?id=60515

... and therefore tried to change the request to

$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = :username LIMIT 1");
$sql->execute(array(':username',$username));

But still with the same result. Does anyone see what is clearly wrong, or why does this request not work when everyone else did?

Thank you in advance!

+5
source share
2 answers

':username',$username only works in the bindParam () method:

$sql->bindParam(':username', $username, PDO::PARAM_STR);

: http://www.php.net/manual/de/pdostatement.bindparam.php

:

$sql->execute(array(':username' => $username));

:

:

$sql->execute(array($username));

:

$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = ? LIMIT 1");    

? . SQL-, .

+5

, .

$sql->execute(array(':username',$username));

$sql->execute(array(':username' => $username));
+11

All Articles