」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何在PDO中使用通配符安全地綁定像參數?

如何在PDO中使用通配符安全地綁定像參數?

發佈於2025-03-25
瀏覽:906

] How to Safely Bind LIKE Parameters with Wildcards in PDO?
在pdO

從tableName中選擇wrd,其中wrd喜歡'$ partial%'

,了解PDO如何處理此類綁定是至關重要的。 The correct approach depends on your specific requirements.

Option 1: Bind with Partial Wildcard (%) at the End

Yes, this is a valid option:
select wrd from tablename WHERE wrd LIKE '$partial%'
select wrd from tablename WHERE wrd LIKE ':partial%'

where :partial is bound to $partial="somet".

Option 2: Bind with Partial Wildcard (%) Inside the Value

You can also use this approach:
select wrd from tablename WHERE wrd LIKE '$partial%'
select wrd from tablename WHERE wrd LIKE ':partial'

where :partial is bound to $partial="somet%".

Alternative: Use CONCAT Function

If you prefer, you can perform MySQL內部的字符串串聯本身:
select wrd from tablename WHERE wrd LIKE ':partial'
如果您的部分單詞包含您的特殊字符,則可以像evement ose to anding to anding to參數,如果您的部分單詞包含特殊字符。以下代碼證明了這一點:

$ stmt = $ db-> 準備(“從tablename中選擇wrd wers wrd like:術語etaver'''''); $ escaped = str_replace(數組(',',',','_'),陣列(',',','''','_',_'),$ var); $ stmt-> bindparam(':術語',$逃脫);

最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3