How to make a PHP search
Make PHP search easy with this example: Create a search form & connect to your database with PHP to find content.
Creating a PHP Search
Creating a search feature with PHP and MySQL can be an easy way to search through a large database of information. This type of search is commonly used on websites to search for specific pieces of data in a large database. In this tutorial, we will walk through the steps of creating a basic search feature with PHP and MySQL.
The first step is to create a database and table for our search. The database should contain the columns that we want to search through. For our example, we will create a table called “people” which will contain the following columns: id, first_name, last_name, and age. We can use the following code to create the table:
CREATE TABLE people (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
age INT(2)
);
Now that the table is created, we can add data to it. We can use the following code to add some sample data to the table:
INSERT INTO people (first_name, last_name, age)
VALUES ('John', 'Doe', 25);
INSERT INTO people (first_name, last_name, age)
VALUES ('Jane', 'Doe', 28);
INSERT INTO people (first_name, last_name, age)
VALUES ('Joe', 'Smith', 32);
INSERT INTO people (first_name, last_name, age)
VALUES ('Jill', 'Smith', 30);
We now have our database set up and ready to use. The next step is to create the HTML form for the search. The form should contain an input field for the search term and a submit button. We can use the following code to create the form:
<form action="search.php" method="POST">
<input type="text" name="search" placeholder="Search...">
<input type="submit" value="Search">
</form>
The form should now be visible on the page. We can now add the PHP code to search the database. The code should first check if the form has been submitted, and if so, it should retrieve the search term from the form. We can use the following code to do this:
if(isset($_POST['submit'])) {
$search = $_POST['search'];
}
Next, we need to create the query to search the database. The query should use the LIKE keyword to search for the given search term in the database. We can use the following code to create the query:
$query = "SELECT * FROM people
WHERE first_name LIKE '%$search%'
OR last_name LIKE '%$search%'
OR age LIKE '%$search%'";
Finally, we need to execute the query and display the results. We can use the following code to execute the query and display the results:
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// Output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["first_name"]. " " . $row["last_name"]. " - Age: " . $row["age"]. "<br>";
}
} else {
echo "No results";
}
Our search feature is now complete! We have created a basic search feature with PHP and MySQL that can search through a database for a given search term. With some minor adjustments, this code can be used to search through any database and display the results.