問題描述
是否可以在準(zhǔn)備好的 MySQL 語句中將列名作為參數(shù)傳遞?舉個(gè)例子:
Is it possible pass a column name as parameter in a prepared MySQL statement? Take the following example:
UPDATE Images
SET :placement = :imageURL
WHERE ID = :titleID;
PDO 在每個(gè)參數(shù)周圍添加 '
,所以上面的中間行變成:
PDO adds '
around each parameter, so the middle line above becomes:
SET 'Homepage' = '1.jpg'
MySQL 不喜歡哪個(gè).有沒有辦法在 PDO 語句中包含字段名稱的參數(shù)并讓它們接受?
Which MySQL doesn't like. Is there a way to include parameters for fieldnames in PDO statements and have them accepted?
否則我想我將不得不編寫幾個(gè)不同的 PDO 語句,這取決于所選擇的內(nèi)容(?).
Otherwise I guess I'll have to write several different PDO statements, depending on what's been chosen(?).
推薦答案
你需要做這樣的事情:
$column = 'someColumn';
$stmt = $db->prepare("UPDATE tableName SET {$column} = :columnValue WHERE ID = :recordId");
參數(shù)化占位符僅用于值.
Parameterized placeholders are only for values.
我建議您閱讀@YourCommonSense 就您的問題發(fā)表的評論.
I would suggest you read the comment @YourCommonSense posted on your question.
這篇關(guān)于在 PDO 語句中動(dòng)態(tài)更改列名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!