Possible Duplicate:
Can PHP PDO Statements accept the table name as parameter?
Im writing a script that allows users to register and login to my site. Im using PDO prepare and execute to prevent SQL injections.
Building the query manually like this DOES work:
$a_query = "SELECT COUNT(*) FROM ". $login_table . "
WHERE `username` = ". $my_username . "
AND `password = " . $my_hash ;
$result_1 = $db->prepare($a_query);
$result_1->execute();
But when I try to use prepare correctly like this, it does NOT:
$a_query = "SELECT COUNT(*) FROM :table
WHERE `username` = :name
AND `password = :pass ;"
$result_1 = $db->prepare($a_query);
$result_1->bindParam(":table", $login_table);
$result_1->bindParam(":name", $my_username);
$result_1->bindParam(":pass", $my_hash);
$result_1->execute();
The error message I get from $result_1->errorInfo[2] reads:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax
to use near ''customerlogin' WHERE `username` = 'guest' AND `password`
= 'qwerty' at line 1
As you can see, prepare() mysteriously slices off the first part of the query before sending it to mysql.
Can anyone explain to me why and how I can fix this?