mySqli 绑定参数 LIKE 与通配符

时间:2023-03-06
本文介绍了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 与通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:mysqli bind_param() 致命错误 下一篇:检索所有行但第一条记录丢失

相关文章