Home >Backend Development >PHP Tutorial >Building a website: Implementation of search engine_PHP tutorial
It is the wish of every website to have a powerful search engine, and the production of a powerful search engine is quite complicated and difficult. It involves many aspects such as efficiency, accuracy and speed.
The search engine introduced here It will not involve such in-depth research, just precise queries for specific content.
A complex and powerful search engine requires a lot of programming and database skills. Let’s start with a simple search engine. .
How does the search engine work? It receives the given keywords, then searches within the given range, and then returns the search results.
The given keywords may be in the information How does the engine search for any location in the content? The following database statement is used here:
select * from table where (name like '%".$keyword."%');
name is the specific location to search for, usually the field name, like '%".$keyword."%' is pattern matching, which is to find whether there is $keyword in the content. Let's look at an example:
Search for the content in the data table news All titles with the keyword good:
select * from news where (title like '%good%');
This is a precise search, which can find all titles with good in the database, and there is also a kind of ambiguity Search:
select * from news where (title like '%good');
You can also find the results in this way.
Assume that news contains title (title), message (content), user (user) For fields such as etc., the above search range is too narrow, because only the title is searched. What should we do if we want to search for other contents without making the operation too complicated?
We notice any changed values in the program Variables are used for processing. This method also works here. You can transfer the range you want to search as a variable, so you have the following database syntax:
mysql_query("select * from news where ($ name like '%".$keyword."%'));
$name stores the value of the field variable transmitted, and this variable value is completed through the html select drop-down submission form.
What if you want to limit the search results to a certain time range? For example, if you want to find information within 5 days. Do you still remember the database syntax used in the introduction to cookies?
That’s right , the syntax of this union is as follows:
mysql_query("select * from news where ($name like
'%".$keyword."%') and time>date_sub('$time',interval 5 day) ");
Where $time is the current time of the search: $time=date('Y-m-d H:i:s'); time is the field in which the database stores information time.
Now replace 5 with $old:
mysql_query("select * from news where ($name like
'%".$keyword."%') and time>date_sub('$time',interval $old day)");
Similarly, the value of $old is submitted for different limited times through the select submission form, thus completing this quite powerful search engine.
Some more powerful search engines require the cooperation of programming skills. Readers can use the above Principle self-expansion test.