为什么我的MYSQL查询无法在php中工作[关闭]

问题描述 投票:-2回答:1

[您好,我正在为我的网站实现搜索功能,我几乎完成了脚本,但是在使用此代码时遇到错误。

代码

<?php
$conn = new mysqli('localhost', 'root', '', 'dbname');
$string  = $_GET['q'];
$search = trim(str_replace("  ", " ", $string));
$str = htmlspecialchars($search);
$q = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
$search_exp = explode (" ", $q);

foreach($search_exp as $search_exploded){
$i=0;
$i++;
if($i==1){
    $construct .="title LIKE '%$search_exploded%' OR keyword LIKE '%$search_exploded%' OR link LIKE '%$search_exploded%' OR description LIKE '%$search_exploded%' OR paragraph LIKE '%$search_exploded%'";
}
else{
$construct .=" AND title LIKE '%$search_exploded%' OR keyword LIKE '%$search_exploded%' OR link LIKE '%$search_exploded%' OR description LIKE '%$search_exploded%' OR paragraph LIKE '%$search_exploded%'";

}
}
$sql = "SELECT * FROM table WHERE $construct ORDER BY `clicks` DESC";
$rs = $conn->query($sql);
while($row1= $rs->fetch_assoc())
 {
  echo $row1["title"]."<br>".$row1["description"];
 }
?>

错误

致命错误:未捕获的错误:在行上调用成员函数fetch_assoc()

谁能告诉我这段代码有什么问题。

php sql search mysqli
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.