I am using prepared statements to perform a SELECT query to my database, however the nature of the SQL syntax is causing a problem with MySQLi.
When I attempt to prepare:
SELECT user_id FROM Users WHERE email='?';
I am getting an error
Warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement
I know the error is being caused by putting quotation marks around the ? but the problem is that these quote marks are required for the SQL syntax to be correct. If I remove the quotes, the prepared statement works, but the database doesn't return what it is supposed to.
Is there something I can do, or is this a limitation of the interface? If it's a limitation, what can I do to perform this query with the safety of prepared statements?
Full Code:
$email = $_POST["email"];
$sql = "SELECT user_id,fname,city,state,zip FROM Users WHERE email='?';";
$types = 's';
$stmt = $db_obj->prepare($sql);
if (!mysqli_stmt_bind_param($stmt, $types, $email)) {
echo "SQL Bind Parameter error.<br/>";
exit;
}
if (!mysqli_stmt_execute($stmt)) {
echo "SQL Statement Execute error.<br/>";
exit;
}
if (!isset($row[0]))
exit ("No such email registered.");