duanjian7343 2016-02-05 13:45
浏览 21
已采纳

如何在mysql中应用多字搜索

I need some points on how to make my php script to look not only 1 word or multiple like ( John Doe ). Currently my script only will find the 1st word "John" from the pointed table. Here is my script:

$query = $_GET['query'];   
$min_length = 1;

if (strlen($query) >= $min_length) { 
    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query);

    $raw_results = mysql_query(
        "SELECT * FROM filmi WHERE  (`title` LIKE '%".$query."%') 
         OR (`description` LIKE '%".$query."%') OR (`nomer` LIKE '%".$query."%')"
    ) or die(mysql_error());

    if (mysql_num_rows($raw_results) > 0) { 
        while ($results = mysql_fetch_array($raw_results)) {
            echo "


            print results       

            ";
        }
    } else { 
        echo "No results";
    }
} else { 
    echo "Minimum length is " . $min_length;
}
  • 写回答

1条回答 默认 最新

  • dragon7713 2016-02-05 14:00
    关注

    Use this code, I have added new line.

    This basically added wild cards % in place of space so that it will modify existing query and adds wildcard to it. This way it can find all records containing all words in the given search query.

    <?php
    $query = $_GET['query']; 
    
    $min_length = 1;
    
    if(strlen($query) >= $min_length){
        $query = htmlspecialchars($query); 
        $query = mysql_real_escape_string($query);
        //added new line ################
        $query = str_replace(' ', '%', $query);
        $raw_results = mysql_query("SELECT * FROM filmi
            WHERE  (`title` LIKE '%".$query."%') OR (`description` LIKE '%".$query."%') OR (`nomer` LIKE '%".$query."%')") or die(mysql_error());
    
        if(mysql_num_rows($raw_results) > 0){ 
            while($results = mysql_fetch_array($raw_results)){
                echo "
                print results       
                ";
            }
        }
        else{ 
            echo "No results";
        }
    }
    else{ 
        echo "Minimum length is ".$min_length;
    } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?