In this tutorial we are going to learn how can search multiple words from Mysql table in a single query in php. You have show any web site there is on search textbox available for search data from that website. User enter the search query and it will result search result. This type of things we will learn in this post. For this things I have make one form with one textbox for entering search query and one button for search. When user click on search button use’s request will send to server and on server side if user enter more than one words than that words will be converted into array by using explode() function and from that array I will make search string with mysql LIKE operator with that array and making complete search query for more than one words.
index.php
<?php | |
$connect = mysqli_connect("localhost", "root", "", "auto_refresh"); | |
if(isset($_POST["submit"])) | |
{ | |
if(!empty($_POST["search"])) | |
{ | |
$query = str_replace(" ", "+", $_POST["search"]); | |
header("location:index.php?search=" . $query); | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Search multiple words at a time in Mysql php</title> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> | |
</head> | |
<body> | |
<br /><br /> | |
<div class="container" style="width:500px;"> | |
<h3 align="center">Search multiple words at a time in Mysql php</h3><br /> | |
<form method="post"> | |
<label>Enter Search Text</label> | |
<input type="text" name="search" class="form-control" value="<?php if(isset($_GET["search"])) echo $_GET["search"]; ?>" /> | |
<br /> | |
<input type="submit" name="submit" class="btn btn-info" value="Search" /> | |
</form> | |
<br /><br /> | |
<div class="table-responsive"> | |
<table class="table table-bordered"> | |
<?php | |
if(isset($_GET["search"])) | |
{ | |
$condition = ''; | |
$query = explode(" ", $_GET["search"]); | |
foreach($query as $text) | |
{ | |
$condition .= "blog_title LIKE '%".mysqli_real_escape_string($connect, $text)."%' OR "; | |
} | |
$condition = substr($condition, 0, -4); | |
$sql_query = "SELECT * FROM tbl_blog WHERE " . $condition; | |
$result = mysqli_query($connect, $sql_query); | |
if(mysqli_num_rows($result) > 0) | |
{ | |
while($row = mysqli_fetch_array($result)) | |
{ | |
echo '<tr><td>'.$row["blog_title"].'</td></tr>'; | |
} | |
} | |
else | |
{ | |
echo '<label>Data not Found</label>'; | |
} | |
} | |
?> | |
</table> | |
</div> | |
</div> | |
</body> | |
</html> |
tbl_blog.sql
-- | |
-- Table structure for table `tbl_blog` | |
-- | |
CREATE TABLE IF NOT EXISTS `tbl_blog` ( | |
`blog_id` int(11) NOT NULL AUTO_INCREMENT, | |
`blog_title` varchar(300) NOT NULL, | |
`blog_link` varchar(100) NOT NULL, | |
PRIMARY KEY (`blog_id`) | |
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ; | |
-- | |
-- Dumping data for table `tbl_blog` | |
-- | |
INSERT INTO `tbl_blog` (`blog_id`, `blog_title`, `blog_link`) VALUES | |
(1, 'How to add live search functionality to your website?', 'https://www.devopsschool.com/blog/how-to-add-live-search-functionality-to-your-website/'), | |
(2, '40+ Online Tools & Resources For Web Developers & Designers', 'https://www.devopsschool.com/blog/40-online-tools-resources-for-web-developers-designers/'), | |
(3, 'User registration using nodejs and mysql with example', 'https://www.devopsschool.com/blog/user-registration-using-nodejs-and-mysql-with-example/'), | |
(4, 'Searching, Column Sorting with Pagination using Ajax in Laravel 5.5', 'https://www.devopsschool.com/blog/searching-column-sorting-with-pagination-using-ajax-in-laravel-5-5/'), | |
(5, 'Design Resources For Developers', 'https://www.devopsschool.com/blog/design-resources-for-developers/'), | |
(6, 'How to Create Multiple Role Based Authentication and Access Control in Laravel Application', 'https://www.devopsschool.com/blog/how-to-create-multiple-role-based-authentication-and-access-control-in-laravel-application/'), | |
(7, 'How to do Auto Load and Refresh Div every Seconds with jQuery and Ajax with PHP Script.', 'https://www.devopsschool.com/blog/how-to-do-auto-load-and-refresh-div-every-seconds-with-jquery-and-ajax-with-php-script/'), | |
(8, 'Laravel – Remove Public from URL using htaccess', 'https://www.devopsschool.com/blog/laravel-remove-public-from-url-using-htaccess/'), | |
(9, 'How to send pdf file through phpmailer?', 'https://www.devopsschool.com/blog/how-to-send-pdf-file-through-phpmailer/'); | |




I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND