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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看