[英]Need a PHP MySQL script to search for keywords in a database
I need to implement a search option for user comments that are stored in a MySQL database. 我需要为存储在MySQL数据库中的用户注释实现搜索选项。 I would optimally like it to work in a similar manner to a standard web page search engine, but I am trying to avoid the large scale solutions.
我最希望它以与标准网页搜索引擎类似的方式工作,但是我试图避免使用大型解决方案。 I'd like to just get a feel for the queries that would give me decent results.
我只是想了解一下可以给我带来体面的结果的查询。 Any suggestions?
有什么建议么? Thanks.
谢谢。
It's possible to create a full indexing solution with some straightforward steps. 可以通过一些简单的步骤来创建完整的索引解决方案。 You could create a table that maps words to each post, then when you search for some words find all posts that match.
您可以创建一个将单词映射到每个帖子的表,然后在搜索某些单词时找到所有匹配的帖子。
Here's a short algorithm: 这是一个简短的算法:
As an entry point, you can use MySQL LIKE queries. 作为入口,可以使用MySQL LIKE查询。
For example if you have a table 'comments' with a column named 'comment', and you want to find all comments that contain the word 'red', use: 例如,如果您有一个表“ comments”和一个名为“ comment”的列,并且想要查找包含单词“ red”的所有注释,请使用:
SELECT comment FROM comments WHERE comment LIKE '% red %';
Please note that fulltext searches can be slow, so if your database is very large or if you run this query a lot, you will want to find an optimized solution, such as Sphinx (http://sphinxsearch.com). 请注意,全文搜索可能会很慢,因此,如果您的数据库很大,或者您经常运行此查询,则需要找到一种优化的解决方案,例如Sphinx(http://sphinxsearch.com)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.