mySqli使用通配符绑定参数LIKE [英] mySqli Bind Parameter LIKE with Wildcard
本文介绍了mySqli使用通配符绑定参数LIKE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在将LIKE
与通配符绑定到我在MySQLi中准备好的语句中时遇到问题.我尝试了以下两种方法,如下所示& concat.(使用@fancyPants输入进行更新)
I'm having issues binding the LIKE
with Wildcard into my prepared statement in MySQLi. I tried both the following methods below as shown & concat.(updated with @fancyPants input)
-
有没有一种方法可以使我在绑定发生后查看自己的SQL语句?
Is there a way so that I can view my own SQL statement after the binding happens?
如何正确绑定以获得所需的结果?
How do I bind it properly to get the result I want ?
它可以在不使用LIKE语句的情况下工作.
It works without the LIKE statement.
我只能从某个搜索词中提取数据.我的代码有什么问题吗?
I could only pull out data from using a certain search term. Is there anything wrong with my code?
$str = $_POST["searchstr"];
if(isset($_POST['submit']))
{
$price=$_POST['price'];
if(!empty($_POST['chkbx']))
{
foreach($_POST['chkbx'] as $selected)
{
$sql= 'SELECT bookTitle, bookPrice FROM nbc_book WHERE catID LIKE "%'.$selected.'%" AND bookTitle LIKE "%'.$str.'%" AND bookPrice < ?';
$stmt=mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt,"i",$price);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $bookTitle, $bookPrice);
while ($stmt->fetch()) {
echo $bookTitle.$bookPrice."<br>";
}
}
}
}
推荐答案
$searchStr = 'oracle';
$sql= 'SELECT bookTitle, bookPrice FROM nbc_book WHERE catID LIKE ? AND bookTitle LIKE "%'.$searchStr.'%" AND bookPrice < ?';
$stmt=mysqli_prepare($con,$sql);
mysqli_stmt_bind_param($stmt,"ssi",$selected,$price);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $bookTitle, $bookPrice);
while ($stmt->fetch()) {
echo $bookTitle;
}
这篇关于mySqli使用通配符绑定参数LIKE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文