dpba63888 2014-07-17 06:08
浏览 161
已采纳

搜索多个关键字

I have this little php search script to help me search through my users table.

Example:

  • If I search for "John", I get results
  • If I search for "John Doe" (two words with a space) I get no results. Even if the users name is John Doe.

I was wondering if there is something in this script I could change to help me search for multiple key words.

Here is the Search code:

<?php
if(isset($_GET['keywords'])){
    $keywords = escape($_GET['keywords']);

    $search = DB::getInstance()->query("
    SELECT `id`,`username`,`first_name`,`last_name`,`unit`,`email`,`rent_own`,`city`,`zip`,`phone`,`joined`,`group_id` FROM `users` WHERE 
    `username`    LIKE '%{$keywords}%' OR
    `first_name`  LIKE '%{$keywords}%' OR
    `last_name`   LIKE '%{$keywords}%' OR
    `unit`        LIKE '%{$keywords}%' OR
    `email`       LIKE '%{$keywords}%' OR
    `rent_own`    LIKE '%{$keywords}%' OR
    `city`        LIKE '%{$keywords}%' OR
    `zip`         LIKE '%{$keywords}%' OR
    `phone`       LIKE '%{$keywords}%' OR
    `joined`       LIKE '%{$keywords}%' OR
    `group_id`    LIKE '%{$keywords}%'  
    ");                



?>

Any thought or solutions are welcome and appreciated.

  • 写回答

2条回答 默认 最新

  • douyu0792 2014-07-17 06:16
    关注

    If you're searching by the full name you'll need to add a condition for it

    CONCAT(`first_name`,' ',`last_name`) LIKE '%{$keywords}%' OR
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数