問題描述
希望有人能幫助我了解當(dāng)變量值旁邊需要多字符通配符時如何在 mysqli 中綁定參數(shù).我發(fā)現(xiàn)它在創(chuàng)建 SQL 語句時對我有用,如下所示:
Hope Somebody will help me about how I bind a parameter in mysqli when a multiple character wildcard needs to be next to the variable value. I found that it worked for me when creating a SQL statement, like this:
$sql = "SELECT item_title FROM item WHERE item_title LIKE '%$title%'";
但是,我嘗試按照相同的模式綁定變量,發(fā)現(xiàn)它失敗了.他們使用了這個代碼:
However, I tried to bind the variable following the same pattern, and found that it failed. They used this code:
$sql = "SELECT item_title FROM item WHERE item_title LIKE '%?%'";
它引發(fā)了這個錯誤:
警告:mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]:變量數(shù)與準(zhǔn)備好的參數(shù)數(shù)不匹配在線program_name中的語句......
Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: Number of variables doesn't match number of parameters in prepared statement in program_name on line......
誰能告訴我如何解決這個問題?謝謝你.
Can anybody tell me how fix this problem? Thank you.
推薦答案
您只能綁定數(shù)據(jù)文字,不能綁定任意查詢部分.
所以,先準(zhǔn)備好你的文字
You can bind only data literals not arbitrary query parts.
So, prepare your literal first
$var = "%$var%";
$sql = "SELECT item_title FROM item WHERE item_title LIKE ?";
這篇關(guān)于PHP 綁定通配符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!