關於pdo引用傳參,欄位是不是不能當成參數傳入?

來源:互聯網
上載者:User
$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;

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.