$pdo= new PDO ( 'mysql:host=localhost;dbname=test;', 'root', '123456', array (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) );$stmt=$pdo->prepare('select ? from tianya_post where id=1');$stmt->bindValue(1, 'title');$stmt->execute();print_r($stmt->fetch());
第一行記錄的標題是:“我和嫂子的那些事兒”
但,列位看官,猜下結果是什嗎?
Array
(
[title] => title
[0] => title
)
為什麼會這樣?
如果用
select title from tianya_post where id=?
$stmt->bindValue(1, 1);
可以得到正確的標題的。
回複討論(解決方案)
欄位列表不是參數!
http://blog.csdn.net/fdipzone/article/details/22330345
需要注意的是以下幾種情況,PDO並不能協助你防範SQL注入。
不能讓預留位置 ? 代替一組值,這樣只會擷取到這組資料的第一個值,如:
select * from table where userid in ( ? );
如果要用in?尋找,可以改用find_in_set()實現
$ids = '1,2,3,4,5,6';
select * from table where find_in_set(userid, ?);
不能讓預留位置代替資料表名或列名,如:
select * from table order by ?;
不能讓預留位置 ? 代替任何其他SQL文法,如:
select extract( ? from addtime) as mytime from table;