Php 如何将参数动态绑定到SQL IN()子句?

Php 如何将参数动态绑定到SQL IN()子句?,php,data-binding,mysqli,Php,Data Binding,Mysqli,searchValue来自ajax搜索栏。 示例值:鸡很胖 //Turns search value into array and counts elements. Or words in the string $explode = explode(' ', $searchValue); //value = Array (chickens, are, fat) $tags_cnt = count($explode); //value = 3 $cnt_q = NULL; $s_cnt = NU

searchValue来自ajax搜索栏。 示例值:鸡很胖

//Turns search value into array and counts elements. Or words in the string
$explode = explode(' ', $searchValue); //value = Array (chickens, are, fat)
$tags_cnt = count($explode); //value = 3

$cnt_q = NULL;
$s_cnt = NULL;
for ($i=0; $i<$tags_cnt; $i++) {

    //$cnt_q finds number of '?' needed for SQL
    $cnt_q = $cnt_q. '?';
    if ($i<$tags_cnt - 1) $cnt_q = $cnt_q.','; //value = ?,?,?

    //$s_cnt finds Number of 'S' for paramater binding
    $s_cnt = $s_cnt. 's'; //value = sss
}

//Turns the Array into comma separated string for Bindparam.
$tags=NULL;
    foreach ($explode as $tag) {
    $tags = $tags."'".$tag."',";
}
$tags = trim($tags, ",");// value = 'Chickens', 'are', 'fat'


//The IN clause = IN(?,?,?)
IN (".$cnt_q.")

//prepared binding statments = $stmt->bind_param('sss','chickens','are','fat');
$stmt = $conn->prepare($query);
$stmt->bind_param($s_cnt, $tags);
//将搜索值转换为数组并对元素进行计数。或者字符串中的单词
$explode=explode(“”,$searchValue)//值=数组(鸡、鸡、脂肪)
$tags\u cnt=计数($explode)//值=3
$cnt_q=NULL;
$s_cnt=NULL;
对于($i=0;$iprepare($query);
$stmt->bind_param($s_cnt,$tags);
我不明白这为什么不起作用

我有SQL查询所需的正确数量的“?”

我有bind_param函数所需的正确数量的“s”

对于bind_param函数,我有正确的搜索值格式:“鸡”、“是”、“脂肪”

我的错误消息: 警告:mysqli_stmt::bind_param():类型定义字符串中的元素数与…行号中的绑定变量数不匹配

下面是完整代码:

//retrive search value from ajax
$searchValue=$_GET["searchValue"];


//connect
$conn = new mysqli($host, $user, $password, $database);
if ($conn->connect_errno) {
    echo "Our apologies, having technical difficulties";
}

//assign tags, turn searchValue into a array by each word
$explode = explode(' ', $searchValue);
$tags_cnt = count($explode);

$cnt_q = NULL;
$s_cnt = NULL;
for ($i=0; $i<$tags_cnt; $i++) {

    $cnt_q = $cnt_q. '?';
    if ($i<$tags_cnt - 1) $cnt_q = $cnt_q.',';

    $s_cnt = $s_cnt. 's';
}

$tags=NULL;
foreach ($explode as $tag) {
    $tags = $tags."'".$tag."',";
}
$tags = trim($tags, ",");


//search by Tags 
$query = "  SELECT results.Title, results.tags
        FROM results
        INNER JOIN tags
        ON results.ID = tags.book_id 
        WHERE tags.tag 
        IN (".$cnt_q.")
        GROUP BY results.ID";


$stmt = $conn->prepare($query);
$stmt->bind_param($s_cnt, $tags);
$stmt->execute();
$results = $stmt->get_result();
    while($row = $results->fetch_array(MYSQLI_ASSOC)) {
        echo "Title: ";
        echo $row["Title"];
        echo "</br>";
        echo "Tags: ";
        echo $row["tags"];
        echo "</br></br> ";
    }



$stmt->close();

$conn->close();
//从ajax检索搜索值
$searchValue=$\u获取[“searchValue”];
//连接
$conn=newmysqli($host、$user、$password、$database);
如果($conn->connect\u errno){
回应“我们的道歉,有技术困难”;
}
//分配标记,按每个单词将searchValue转换为数组
$explode=explode(“”,$searchValue);
$tags\u cnt=计数($explode);
$cnt_q=NULL;
$s_cnt=NULL;
对于($i=0;$ibind_参数($s_cnt,$tags);
$stmt->execute();
$results=$stmt->get_result();
而($row=$results->fetch_数组(MYSQLI_ASSOC)){
回声“标题:”;
echo$行[“标题”];
回声“
”; 回声“标签:”; echo$行[“标记”]; 回声“

”; } $stmt->close(); $conn->close();
试试这个,它应该可以正常工作:

$searchValue=$\u GET[“searchValue”];
//连接
$conn=newmysqli($host、$user、$password、$database);
if($conn->connect\u errno){
回应“我们的道歉,有技术问题”}

//分配标记,按每个单词将searchValue转换为数组

$explode=explode(“”,$searchValue);
$tags\u cnt=count($explode);

$cnt_q=NULL;
$s_cnt=NULL;
对于($i=0;$iget_result());
而($row=$results->fetch_数组(MYSQLI_ASSOC)){
回声“标题:”;
echo$行[“标题”];
回声“
”; 回声“标签:”; echo$行[“标记”]; 回声“

”;
}

$stmt->close();


$conn->close();

尝试此代码,不要使用不必要的循环

$seachValue = isset($_GET['searchValue']) ? $_GET['searchValue'] : '';
if (!$seachValue) {
    exit('Empty search value');//Your error text
}

$values = explode(' ', $seachValues);
$size = count($values);

$markers = $params = array();
$markers = array_pad($markers, $size, '?');
$params = array_pad($params, $size, 's');

$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_errno) {
    exit('Our apologies, having technical difficulties');
}

$query = 'SELECT results.Title, results.tags
    FROM results
    INNER JOIN tags
    ON results.ID = tags.book_id 
    WHERE tags.tag 
    IN (' . implode(',', $markers) . ')
    GROUP BY results.ID';
$stmt = $mysqli->prepare($query);
if (!$smtp) {
    exit('Our apologies, having technical difficulties');//Your error text
}

$tags = array(implode($params));
for ($i = 0; $i < $size; ++$i) {
     $tags[] = &$values[$i];
}

call_user_func_array(array($stmt, 'bind_param'), $tags);
$stmt->execute();

$results = $stmt->get_result();
if ($results->num_rows > 0) {
    foreach ($row = $results->fetch_assoc()) {
        echo sprintf('Title: %s<br>Tags: %s<br><br>', $row["Title"], $row["tags"]);
    }
}
else {
    echo 'Nothing found';
}

$stmt->close();
$mysqli->close();
$seachValue=isset($\u GET['searchValue'])?$\u GET['searchValue']:'';
如果(!$seachValue){
退出('空搜索值');//错误文本
}
$values=爆炸(“”,$seachValues);
$size=计数($value);
$markers=$params=array();
$markers=array_pad($markers,$size,“?”);
$params=array_pad($params,$size,'s');
$mysqli=newmysqli($host、$user、$password、$database);
如果($mysqli->connect\u errno){
退出(“我们的道歉,有技术问题”);
}
$query='选择results.Title,results.tags
根据结果
内部联接标记
ON results.ID=tags.book\u ID
tags.tag在哪里
在(“.内爆(“,”,$markers)。”
按结果分组;
$stmt=$mysqli->prepare($query);
如果(!$smtp){
退出(“我们的道歉,有技术问题”);//您的错误文本
}
$tags=array(内爆($params));
对于($i=0;$i<$size;++$i){
$tags[]=&$values[$i];
}
调用_user_func_数组(数组($stmt,'bind_param'),$tags);
$stmt->execute();
$results=$stmt->get_result();
如果($results->num_rows>0){
foreach($row=$results->fetch\u assoc()){
echo sprintf('Title:%s
标记:%s
,$row[“Title”],$row[“Tags”]); } } 否则{ 回声“什么也没找到”; } $stmt->close(); $mysqli->close();
当您转储查询时,结果是什么?$s_cnt的数量与$tags不同。您是否尝试打印它们的数量?是的。我已经打印出来,它们是相同的。我得到:sss-、?、?-‘鸡’、‘是’、‘胖’不起作用。非常确定每个参数都需要相同数量的?和。所以三个词需要三个?一个d三S。当你尝试我的方式时,你会遇到什么错误?我已经在我的服务器上做了测试,它工作得很好,没有错误。如果输入了多个单词,它将不起作用。例如:“Chicken”起作用。但是“ABC Chicken”不起作用。我编辑了我的答案,我想现在它会以你想要的方式工作。谢谢你。起作用。我会摆弄它,然后你知道为什么吗,要等22小时才能拿到赏金。