dtkago3208 2014-05-12 11:41
浏览 57

使用全文而不是LIKE php mysql即时搜索

I want to do an instant search for my website using jquery php and mysql. Previously i used to take the text from .searchbox on keyup and send request to php via ajax.

PHP

$query="Select from names where concat(fname," ",lname) Like '$search%' "

this would work fine. But i heard Fulltext is tons time faster than LIKE. But its happens to work only if someone writes a full name . Therefore fails to autocomplete searches. I wonder whether is there any alternative to my above code which is way much faster using fulltext or any other means.

  • 写回答

2条回答 默认 最新

  • douyan3478 2014-05-12 12:29
    关注

    I suggest

    in PHP

    $search_name=explode(" ", addslashes($search));
    fname =  $search_name[0];
    if(!empty($search_name[1])) { 
        $lname = $search_name[1];
        $sql_add = "(lname LIKE '$lname%') OR";
    } else $sql_add="";
    

    in sql

    "Select from names where $sql_add fname LIKE '$fname%'"
    

    It is true that maybe will have a lot of results, but that is better than some of the results are lost because of sql condition.

    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗