dqouryz3595 2015-09-14 21:11
浏览 25
已采纳

搜索确切的关键字

I am trying to make a search script that searches cars in a database and matches ALL keywords input by user. If I leave keywords text box empty and search I get results but if I input any keywords I get no results.

$search_keywords = $_POST['search_keywords'];
$terms =  $search_keywords;

$items = explode(' ',$terms);
$types = array();
    foreach ($items as $item) {
        $types[] = "'title' LIKE '%{$item}%'";
        $types[] = "'exterior_colour' LIKE '%{$item}%'";
    }

    $sql = "SELECT * FROM list_car WHERE ";
    $sql .= implode(" && ", $types) . " ORDER BY 'title'";

    $result = mysqli_query($link, getPagingQuery($sql, $rowsPerPage));

UPDATE:

Works now i have changed it but if i search Toyota hilux dfkjodsgfudsugfdsgfgfgfdgfdg all the Toyota hilux will appear but dfkjodsgfudsugfdsgfgfgfdgfdg is garbage which is not listed in the database i want it to match ALL keywords not just one or more.

$search_keywords = $_POST['search_keywords'];
$terms =  $search_keywords;

$items = explode(' ',$terms);
$types = array();
    foreach ($items as $item) {
        $types[] = "`title` LIKE '%{$item}%'";
        $types[] = "`exterior_colour` LIKE '%{$item}%'";
    }


    $sql = "SELECT * FROM list_CAR WHERE ";
    $sql .= implode(" || ", $types) . "";

    $result = mysqli_query($link, getPagingQuery($sql, $rowsPerPage)) or die(mysqli_error($link));
  • 写回答

2条回答 默认 最新

  • duanrebo3559 2015-09-14 23:15
    关注

    Here's how I'd do it.

    $terms = $search_keywords = 'Toyota hilux dfkjodsgfudsugfdsgfgfgfdgfdg';
    $items = explode(' ',$terms);
    $types = array();
    $sql = "SELECT * FROM list_CAR WHERE ";
    foreach ($items as $item) {
        $sql .= " (`title` LIKE ? or `exterior_colour` LIKE ?) and ";
        $params[] = '%' . $item . '%';
        $params[] = '%' . $item . '%';
    }
    if(!empty($params)) {
         $sql = rtrim($sql, ' and ');
         $result = mysqli_prepare($link, $sql);
         foreach($params as $param) {
             mysqli_stmt_bind_param($result, "s", $param);
         }
         mysqli_stmt_execute($result);
    } else {
         die('No params built...WHY');
    }
    

    Note I'm using untested mysqli prepared statements, I haven't built the parameterized queries procedurally in mysqli, I base this approach off user comments on the manual's page.

    This should give a query such as

    SELECT * FROM list_CAR 
    WHERE  
    (`title` LIKE ? or `exterior_colour` LIKE ?) and  
    (`title` LIKE ? or `exterior_colour` LIKE ?) and  
    (`title` LIKE ? or `exterior_colour` LIKE ?)
    

    Which will require each keyword is present in the title or the color list.

    If you were to keep it unprepared, which is unrecommended and poor practice, it would be..

    $terms = $search_keywords = 'Toyota hilux dfkjodsgfudsugfdsgfgfgfdgfdg';
    $items = explode(' ',$terms);
    $types = array();
        foreach ($items as $item) {
            $types[] = " (`title` LIKE '%{$item}%' or `exterior_colour` LIKE '%{$item}%') ";
        }
        $sql = "SELECT * FROM list_CAR WHERE ";
        $sql .= implode(" and ", $types) . "";
    echo $sql;
    

    Output:

    SELECT * FROM list_CAR WHERE  
    (`title` LIKE '%Toyota%' or `exterior_colour` LIKE '%Toyota%')  and  
    (`title` LIKE '%hilux%' or `exterior_colour` LIKE '%hilux%')  and 
     (`title` LIKE '%dfkjodsgfudsugfdsgfgfgfdgfdg%' or `exterior_colour` LIKE '%dfkjodsgfudsugfdsgfgfgfdgfdg%')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题