dongshen9686 2015-07-11 01:23
浏览 14

如何在sql中搜索包含字符串的标题并将结果推送到数组中

Okay, I'm quite new to Mysql and I can't find a simple solution to my question. I want to search my database and return all instances of a string. ex: user types "hello world" and database returns queries of "hello my name is world" etc. I'll take these results and push them into a php array.

here's my attempt of it according to this:

include '../config.php';
$con = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME .'', DB_USER,DB_PASSWORD);

$firstsearcharray = array(); // push into this array
$searchqueryarray = explode(" ",$searchquery);//$searchquery is data sent from AJAX
$query = $con->prepare("SELECT * FROM srating where MATCH(title) AGAINST (':searchqueryarray')");
$query->bindParam(':$searchqueryarray',$searchqueryarray);
$query->execute();
$data = $query->fetch(PDO::FETCH_ASSOC);//I have to somehow push each result into an array (possibly using array_push and a for loop)

I believe that the way I'm searching isn't optimal. I would prefer to use CONTAINS over LIKE as I've heard that it is faster, but it is way less documented than LIKE.

  • 写回答

1条回答 默认 最新

  • douzao2590 2015-07-11 01:52
    关注

    This is a solution for fulltext search.

    An example:

    SELECT id,prod_name, match( prod_name )
    AGAINST ( '+harpoon +article' IN NATURAL LANGUAGE MODE) AS relevance
    FROM testproduct 
    ORDER BY relevance DESC
    

    http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

    Like and contains are a waste of time.

    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装