It is very dangerous to pass dynamically constructed table names in a query. But if it is so necessary for your application, you must misinform the data. Since PDO cannot handle this, you must call mysql_real_escape_string on the table name yourself. You will also have to wrap the table name with reverse records as `table_name`. Therefore, prepare the request as:
'SELECT * FROM `' . mysql_real_escape_string($database) . '` WHERE id = :id
One note: mysql_real_escape_string
requires an already established database connection.
EDIT: But when I think about it, it's probably best to map the $database
variable to your existing tables.
Martin Dimitrov
source share