Home >Backend Development >PHP Tutorial >How to implement a simple search engine using PHP

How to implement a simple search engine using PHP

WBOY
WBOYOriginal
2023-09-26 14:01:021856browse

How to implement a simple search engine using PHP

How to use PHP to implement a simple search engine

Search engines play an important role in modern society, helping users quickly find the information they need. This article will introduce how to use the PHP programming language to implement a simple search engine and provide specific code examples.

First, we need a database to store the content to be searched. We can use the MySQL database to create a database named "search_engine" and create a data table named "documents" within the database to store document content and titles. Here is the SQL code used to create the data table:

CREATE TABLE `documents` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Next, we need to create an HTML page for searching. The following is a simple search page sample code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>搜索引擎</title>
</head>
<body>
    <form action="search.php" method="GET">
        <input type="text" name="query" placeholder="请输入搜索关键词">
        <input type="submit" value="搜索">
    </form>
</body>
</html>

In the above code, we created a form containing a text input box and a submit button. The user can enter search keywords in the text input box. Then click the submit button to search.

Next, we need to create a PHP file that will handle the search request and return relevant search results. Here is a code example for a simple search.php file:

<?php
// 连接到MySQL数据库
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "search_engine";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查数据库连接是否成功
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}

// 获取用户输入的搜索关键词
$query = $_GET['query'];

// 从数据库中查询包含搜索关键词的文档
$sql = "SELECT * FROM `documents` WHERE `content` LIKE '%$query%' OR `title` LIKE '%$query%'";
$result = $conn->query($sql);

// 输出搜索结果
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "标题: " . $row["title"]. "<br>";
        echo "内容: " . $row["content"]. "<br> <hr>";
    }
} else {
    echo "没有找到匹配的结果";
}

// 关闭数据库连接
$conn->close();
?>

In the above code, we first connect to the pre-created "search_engine" database. Then, we obtain the search keywords entered by the user from the HTML page and use SQL query statements to retrieve documents containing the search keywords from the database. Finally, we use loops and conditional statements to output the search results.

By saving the above code as a search.php file and placing it in the same directory as the previously created HTML search page, we can access the HTML page in the browser and enter the search keywords , and get relevant search results.

It should be noted that the above example only shows how to use PHP to implement a simple search engine, and the code does not do any security processing. In actual development, we also need to verify and filter input to prevent the submission of malicious content and security issues such as SQL injection.

I hope this article can help readers understand how to use PHP to implement a simple search engine, and help readers get started by providing specific code examples. Through further study and improvement, readers can achieve more powerful and flexible search engine applications.

The above is the detailed content of How to implement a simple search engine using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn