本文介紹了PDO 和 MySQL IN/NOT IN 查詢的正確格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
出于顯而易見的原因,這是要尋找的謀殺...
For reasons that should be obvious, this is murder to search for...
我如何在 PDO 中執行此操作:
How do I do this in PDO:
SELECT thing FROM things WHERE thing_uid IN ( ... )
我的特殊用例是一個字符串,它是通過分解從具有幾十個復選框的表單中獲取的數組構建的.在標準 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 的反注入保護中受益……綁定參數等等.那么我該怎么做呢?
but I want that to benefit from PDO's anti-injection protection... bound params and all that. So how can I do it?
推薦答案
創建一個包含與您擁有的值一樣多的 ?
的數組,并將其放入查詢中.
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) . ")";
這篇關于PDO 和 MySQL IN/NOT IN 查詢的正確格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!