douji1999 2019-03-29 16:05
浏览 127

PHP准备了多个LIKE条件的SQL

I want to do a search in a table with search words defined by a user. I'm doing this by splitting the string an constructing the sql.

But i can't seem to make it work. It works fine, if only one word is entered, but with two or more words it's crashing.

$q = $_GET['q']; //Search word
$q = htmlspecialchars($q);
$q_exploded = explode ( " ", $q );
foreach( $q_exploded as $search_each ) {         
    $where .= "content LIKE ? OR ";
    $bind  .= "s";
    $param .= "%$search_each%, ";
} 

$where = rtrim($where,'OR ');
$param = rtrim($param,', ');

$sql = "SELECT ads_id FROM search_index WHERE ".$where."";
echo $sql . "<br>".$param."<br>".$bind."<br>";

$stmt = $dbconn->prepare($sql);
$stmt->bind_param($bind, $param);
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    echo $row['ads_id'];
}

This is my error

SELECT ads_id FROM search_index WHERE content LIKE ? OR content LIKE ?

%word1%, %word2%

ss

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

  • 写回答

2条回答 默认 最新

  • douba1904 2019-03-29 16:10
    关注

    You issue is here:

    $stmt->bind_param($bind, $param);
    

    What you're doing is:

    $stmt->bind_param("ss", $param);
    

    While you may intend param to satisfy both of the strings it doesn't you need to pass a variable for each one. I would try looking into explode for this.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错