简体   繁体   中英

PDO : Select using a prepared statement returns column name

I am trying to use a prepared statement where the column is prepared

ie

SELECT ? FROM users

Now this normally works if I put

SELECT id FROM users

But doing the first statement, the value is the column name.

id = id
0 = 0

What am I doing wrong, or is this possible?

No you can't bind column names or table names.

Here's more info Escaping column names in PDO statements

A prepared statement can only replace value in the statement not field nor column name, this is because prepared statement are kind of precompiled and optimized in function of the whole statement except the value.

so this is possible:

SELECT id FROM users WHERE name=?

but not this:

SELECT ? FROM users WHERE name='john'
SELECT id FROM ? WHERE name='john'
SELECT id FROM users WHERE ?='john'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM