I've been using prepared statements over the past two weeks and had no problems. Today, I'm completely baffled as to what I'm doing wrong here. I've been doing somewhat complex queries using the same technique that I am using here.
This is the query I want to perform:
'SELECT username FROM ? WHERE password=?'
After this didn't work I tried boiling it down and trying the simplest version I can use to build up from. Yet that still resulted in a syntax error.
My Current Code:
...
// mysqli object created and connection established
if (!($stmt = $mysqli->prepare('Select * FROM ?')))
{
// Prepared statement for retrieving a user failed
echo 'Prepare failed: (' . $mysqli->errno . ') ' . $mysqli->error;
exit;
}
// Prepared statement for inserting a new user created
if (!($stmt->bind_param('s', $table)))
{
// Paramater binding failed
echo 'Binding parameters failed: (' . $stmt->errno . ') ' . $stmt->error;
exit;
}
...
The Error:
Prepare failed: (1064) 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 '?' at line 1
This is for a school project so if you would like you can give suggestions to make this more secure. I'm using phpass to hash my passwords.
?for table names. Choose an actual table name or use a variable for it.