PHP Search Engine with Match Against query

Create a simple PHP Search Engine using Match Against query

In this tutorial, we are going to create a simple PHP search engine with Match Against query.

To create this PHP Search engine follow the below steps.


Step – 1

First open your phpMyAdmin and create a database called php_search

Database table creation

After creating the database select this php_search Database and go to SQL tab then copy the below SQL code and paste into the SQL text-area and then click on Go button.

This SQL code creates a table called `users` into the php_search Database and it also creates the `users` table structure.

CREATE TABLE `users` (
  `id` int(11) NOT NULL,
  `username` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `users`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
users table structure

Important

But before you perform Match Against Search query you need to add FULLTEXT index to your users-table that column which you want to use.

Here we’ll use username column, for the reason we’ll Add FULLTEXT index to username column.

For adding FULLTEXT index to username column you need to run below SQL query –

ALTER TABLE `users` ADD FULLTEXT KEY `username` (`username`);

So again select the php_search Database and go to SQL tab then copy the above SQL code and paste into the SQL text-area and then click on Go button.


Step – 2

Now time to creating files. So go to your htdocs folder or www folder and create a folder and name it as you wish here I named it php_search.

Inside the php_search folder, we’ll create two files one is config.php and another index.php.


Files creation

config.php

<?php
// Database connection
$dbConn = mysqli_connect("localhost","root","","php_search");

// Check database connection
if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

index.php

<?php
// include database connection
require 'config.php';

if(isset($_GET['s'])){
// escaping special characters
$keyword = mysqli_real_escape_string($dbConn, $_GET['s']);
//query
$query = mysqli_query($dbConn,"SELECT * FROM `users` WHERE MATCH (username) AGAINST ('$keyword')");

}

?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Search Engine</title>

<style type="text/css">

body{
padding: 0;
margin: 0;
font-family: sans-serif;

}
form{
display: block;
margin: 0 auto;
max-width: 400px;
padding: 10px;
}
input[type='text']{
padding: 5px;
width: 100%;
}
.searchResult{
padding: 10px 0;
}
ul{
margin: 0;
padding: 0;
list-style: none;
}
ul li {
padding: 5px 0;
}
</style>
</head>
<body>

<form method="get" action="">
<strong>Search Name</strong><br>
<input type="text" name="s" placeholder="Type Name" required><br><br>
<input type="submit" value="Search">
<div class="searchResult">
<strong>Search Result</strong>
<ul>
<?php
if(isset($query) && mysqli_num_rows($query) > 0){

while ($row = mysqli_fetch_array($query)) {
echo "<li>".$row['username']."</li>";
}

}
?>
</ul>
</div>
</form>

</body>
</html>

Completed.

Leave a Reply

Your email address will not be published. Required fields are marked *