All Tutorials

Your One-Stop Destination for Learning and Growth

Creating a Search Script with PHP and MySQL

Search functionality is an essential feature for websites that have large databases. In this blog post, we will go through the process of creating a simple search script using PHP and MySQL. This example assumes you have a basic understanding of HTML, CSS, and have a table named articles in your MySQL database.

Prerequisites:

  1. A web server with PHP installed (e.g., Apache or Nginx).
  2. MySQL database with a table named articles.
  3. Basic understanding of HTML, CSS, and SQL.

Setting up the Environment:

First, let's create an index.php file to handle user input and display search results.

<?php
// Database connection details
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// Create a connection to the MySQL database
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

Replace "localhost", "username", "password", and "database_name" with your database connection details.

Next, create a basic HTML form to allow users to enter search terms in a new file called search.php.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Search</title>
    <style type="text/css">
        input[type=text] { width: 300px; }
    </style>
</head>
<body>
    <form action="search_results.php" method="get">
        <label for="query">Search:</label>
        <input type="text" name="query" id="query">
        <button type="submit">Search</button>
    </form>
</body>
</html>

Now create a new file search_results.php to handle the search functionality and display results.

Implementing Search Functionality:

Update your index.php file to include the following code before the closing tag. This code handles user input and performs searches using the provided query.

if (isset($_GET['query'])) { // User has submitted a search query
    $search_query = mysqli_real_escape_string($conn, $_GET['query']);

    // Query to fetch articles containing the search term
    $sql = "SELECT * FROM articles WHERE title LIKE '%$search_query%' OR content LIKE '%$search_query%'";
    $result = mysqli_query($conn, $sql);
}
?>

Now update search.php to include the search results when they are available. Add the following code at the end of the file:

<?php
if (isset($_GET['query'])) { // User has submitted a search query
    require 'index.php';

    $search_query = mysqli_real_escape_string($conn, $_GET['query']);
    $sql = "SELECT * FROM articles WHERE title LIKE '%$search_query%' OR content LIKE '%$search_query%'";
    $result = mysqli_query($conn, $sql);
} else { // User is on the search page but hasn't submitted a query yet
?>
<!DOCTYPE html>
<html lang="en">
<!-- Include HTML for the search form here -->
<?php } ?>
</body>
</html>

Make sure to include the proper <head>, <title>, and other necessary HTML tags in your files.

Now, when users submit a search query on the search.php page, the script will perform a search based on the provided term and display results on index.php. You can customize the search functionality further to fit your specific use case.

Published March, 2015