Develop a Custom Search Engine Using PHP and MySQL

0
69

Creating a custom search engine is an essential project for web developers interested in enhancing their knowledge of PHP and MySQL. In this article, we will guide you through building a simple yet functional search engine using PHP and MySQL.

Why Build a Custom Search Engine?

Developing a custom search engine allows you to:

  • Understand the fundamentals of search queries.
  • Enhance database management skills.
  • Create customized search experiences tailored to specific applications.

Follow this video for complete guidance :

Full Source Code

Create a table named “pages” with necessary columns as defined in following SQL query :

CREATE TABLE pages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    url VARCHAR(255) NOT NULL,
    title VARCHAR(255) NOT NULL,
    description TEXT,
    keywords TEXT
);

Then use following source code :

<?php

// Database connection
$conn = new mysqli("localhost", "root", "", "search_engine");

if (isset($_GET['query'])) {
    $query = $conn->real_escape_string($_GET['query']);
    $sql = "SELECT * FROM pages WHERE url LIKE '%$query%' or title LIKE '%$query%' OR description LIKE '%$query%' OR keywords LIKE '%$query%'";
    $result = $conn->query($sql);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Search Engine</title>
    <style>

		@import url('https://fonts.googleapis.com/css2?family=Barlow+Condensed:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900');


        body {
            font-family: Arial, sans-serif;
            background-color: #f8f9fa;
            text-align: center;
            padding-top: 100px;
        }
        .search-box {
            max-width: 800px;
            margin: 0 auto;
        }
        .logo {
            font-size: 64px;
            font-weight: bold;
            color: #4285f4;
            font-family: "Barlow Condensed", serif;;
        }
        .logo span {
            margin-left:10px;
            color: #ea4335;
        }
        .search-form {
            margin-top: 20px;
        }
        input[type="text"] {
            width: 60%;
            padding: 15px;
            font-size: 18px;
            border: 1px solid #dfe1e5;
            border-radius: 24px;
            outline: none;
            box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
        }
        input[type="submit"] {
            padding: 10px 20px;
            font-size: 16px;
            color: white;
            background: #4285f4;
            border: none;
            border-radius: 24px;
            cursor: pointer;
            margin-left: 10px;
        }
        input[type="submit"]:hover {
            background: #357ae8;
        }
        .results {
            margin-top: 40px;
            text-align: left;
            max-width: 800px;
            margin: 0 auto;
        }
        .result-item {
            background: white;
            padding: 20px;
            margin: 10px 0;
            border-radius: 8px;
            box-shadow: 0 1px 6px rgba(32, 33, 36, 0.28);
        }
        .result-item a {
            color: #1a0dab;
            font-size: 20px;
            font-weight: bold;
            text-decoration: none;
        }
        .result-item .url {
            color: #006621;
            font-size: 14px;
            margin: 4px 0;
        }
        .result-item p {
            color: #4d5156;
            margin: 8px 0 0;
        }
    </style>
</head>
<body>
<div class="search-box">
    <div class="logo">Custom<span>Search</span></div>
    <form class="search-form" method="GET" action="">
        <input type="text" name="query" placeholder="Search here..." required>
        <input type="submit" value="Search">
    </form>
</div>

<?php if (isset($result)){ ?>
<div class="results">
    <?php if ($result->num_rows > 0){ ?>
        <?php while ($row = $result->fetch_assoc()){ ?>
            <div class="result-item">
                <a href="<?php echo htmlspecialchars($row['url']);?>" target="_blank">
                    <?php echo htmlspecialchars($row['title']);?>
                </a>
                <div class="url"><?php echo htmlspecialchars($row['url']);?></div>
                <p><?php echo htmlspecialchars($row['description']);?></p>
            </div>
        <?php } ?>
    <?php } else{ ?>
        <p>No results found.</p>
    <?php } ?>
</div>
<?php } ?>
</body>
</html>

Project Overview

Our search engine will query a MySQL database for relevant results based on user input. The search results will display the page title, URL, and description, styled to resemble a professional search interface.

ALSO READ  Free Google language translation API using PHP

Requirements

Before starting, ensure you have the following:

  • A web server with PHP support (e.g., XAMPP or WAMP).
  • MySQL database setup.
  • Basic understanding of PHP, HTML, and CSS.

Setting Up the Database

  • Create a Database: Name it search_engine.
  • Create a Table: Name it pages, with columns id, title, url, description, and keywords.
  • Insert Sample Data: Populate the table with sample pages.

Project Breakdown

  • Database Connection: Connect PHP to MySQL using mysqli.
  • Search Query Execution: Use a query that matches user input against multiple fields (title, description, keywords).
  • Display Results: If matches are found, display them in a styled format with clickable links.
  • User Interface Design: Ensure a clean, user-friendly layout inspired by popular search engines.

Features to Enhance

To further develop this project, consider adding:

  • Pagination for search results.
  • Advanced search filters.
  • Improved relevance ranking using full-text search.
  • Secure input handling to prevent SQL injection.

Building a custom search engine using PHP and MySQL is a practical project for aspiring web developers. It enhances coding skills while offering a valuable, real-world application. Start small, experiment with features, and continuously refine your search engine to improve its performance and user experience.

Comments are closed.