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;
}

相关文章