Just recently I\'ve switched to using PDO in PHP/MySQL and transformed some dozens of queries. Most of them worked, however this very easy one throws an exception at $
The ':username',$username
works only in bindParam() method:
$sql->bindParam(':username', $username, PDO::PARAM_STR);
Take a look here: http://www.php.net/manual/en/pdostatement.bindparam.php
For execute you need to pass a correct array of input-only values:
$sql->execute(array(':username' => $username));
Placeholder:
You can also use this:
$sql->execute(array($username));
But for this you need to change your query to this:
$sql=$pdo->prepare("SELECT `id` FROM `user` WHERE `username` = ? LIMIT 1");
The ? works as palceholder and take the variables from the array. When you use more placeholder in your SQL statement the function takes all the variables out of the array in it's order.
This is not bug, you provide two parameters for only one placeholder.
$sql->execute(array(':username',$username));
should be
$sql->execute(array(':username' => $username));