問(wèn)題描述
出于顯而易見(jiàn)的原因,這是要尋找的謀殺...
For reasons that should be obvious, this is murder to search for...
我如何在 PDO 中執(zhí)行此操作:
How do I do this in PDO:
SELECT thing FROM things WHERE thing_uid IN ( ... )
我的特殊用例是一個(gè)字符串,它是通過(guò)分解從具有幾十個(gè)復(fù)選框的表單中獲取的數(shù)組構(gòu)建的.在標(biāo)準(zhǔn) MySQL 中,這很容易...
My particular use case is a string built by exploding an array taken from a form with several dozen checkboxes. In standard MySQL this is very easy...
$thingString = implode("', '", $thingArray);
$q = "SELECT thing FROM things WHERE thing_uid IN ('$thingString')";
但我希望它能夠從 PDO 的反注入保護(hù)中受益……綁定參數(shù)等等.那么我該怎么做呢?
but I want that to benefit from PDO's anti-injection protection... bound params and all that. So how can I do it?
推薦答案
創(chuàng)建一個(gè)包含與您擁有的值一樣多的 ?
的數(shù)組,并將其放入查詢中.
Create an array of as many ?
as you have values, and throw that into the query.
$placeholders = array_fill(0, count($thingArray), '?');
$sql = "SELECT thing FROM things WHERE thing_uid IN (" . implode(',', $placeholders) . ")";
這篇關(guān)于PDO 和 MySQL IN/NOT IN 查詢的正確格式的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!