Home >Backend Development >PHP Tutorial >How to implement search function on website using PHP
With the popularity and development of the Internet, websites have become one of the important channels for people to obtain information and services. The search function on the website is one of the important tools for users to quickly obtain the information they need. This article gives a brief description of how to use PHP to implement the search function on the website.
1. Basic principles of the search function
To implement the search function on the website, the following four basic steps need to be completed:
1. The user enters keywords to search;
2. The website searches the database based on keywords;
3. The website extracts data that meets the search conditions;
4. The website outputs and presents the data that meets the search conditions.
2. Technical methods to realize the search function
1. The most important technical means to realize the search function is to use SQL language to query the database. In PHP, use database extensions such as mysqli or PDO to connect to the database for query operations. For professional search engine services, you can also use search engine services such as sphinx and Lucene.
2. When implementing the search function, you need to pay attention to the following technical issues:
(1) Prevent SQL injection. Search keywords entered by users need to be filtered and verified to prevent malicious data injection.
(2) Efficiency of search function. For large websites, the search function will query a large amount of data in the database, and the search function needs to be optimized, such as using indexes.
(3) Presentation of search results. The search results on the website should be classified, sorted or paginated according to the user's search needs.
3. Practical combat: PHP implements the search function on the website
Before implementing the search function, you need to configure the backend first database. Assume that our database is named "mydb" and the table is named "news", which stores the title and content fields of news information. We can use the following statement to create a table:
CREATE TABLE IF NOT EXISTS news
(
id
int(11) NOT NULL AUTO_INCREMENT,
title
varchar(100) NOT NULL,
content
text NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET =utf8 AUTO_INCREMENT=1 ;
We need to build an interface for the search function, and users can enter keywords in the search box. Enter keywords in the search box, click the submit button or press the Enter key to jump to the search.php page for processing.
The code is as follows:
3. Query the database to implement the search function
In search.php, we will get the data that matches the search criteria from the database and present it. We need to use PHP's mysqli extension to connect to the database. The code is as follows:
$q = $_GET['q']; //Get the search keyword
if ($q == '') {
echo "Please enter keywords to search";
exit;
}
$con = mysqli_connect('localhost', 'root', '', 'mydb'); //Connect to the database
if (!$con) {
die("Unable to connect to database: " . mysqli_error($con));
}
mysqli_select_db($con, "mydb");//Select database
mysqli_query ($con, "set names 'utf8'");
$sql = "select * from news where title like '%" . $q . "%' or content like '%" . $q . "%' order by id desc";
$result = mysqli_query($con, $sql);
if (!$result) {
printf("Error: %s
", mysqli_error($con ));
exit();
}
while ($row = mysqli_fetch_array($result)) {//Loop to obtain qualified data and present it
echo '
' . $row['content'] . '
';The above code connects to the database through the mysqli extension and queries the database for data that meets the search conditions. This includes the following steps:
(1) Filter and verify the keywords entered by the user to prevent SQL injection;
(2) Use mysqli_connect() to connect to the mydb database on localhost. If Unable to connect, exits with a connection error.
(3) Select the mydb database and set the database encoding to 'utf8'.
(4) Use SQL query statements to query qualified data in the news table, and use $order_rows to sort in descending order.
(5) Loop to obtain the data of the query results and present the title and content on the web page.
(6) Close the database connection.
4. Summary
This article introduces how to use PHP to implement the search function on the website, including the basic principles, technical methods and practical exercises for implementing search. In the demonstration of this article, we use mysqli to query the database, filter and verify the search keywords, and avoid the security risks of SQL injection. By studying this article, you can also conduct more in-depth research on other technical means of search engines.
The above is the detailed content of How to implement search function on website using PHP. For more information, please follow other related articles on the PHP Chinese website!