Introduction
In this article we will learn how to make a search page in PHP. For this we need a textbox and a button control. When the user enters any value for searching a record and clicks on a button control then its corresponding values are shown in a tabular form below the page. It will show No Results found if no records exist for that search.
Table Structure
phpMyAdmin SQL Dump
version 2.10.1
http://www.phpmyadmin.net
Host: localhost
Generation Time: May 20, 2012 at 06:39 PM
Server version: 5.0.45
PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
Database: `Work`
Table structure for table `employee`
CREATE TABLE `employee` (
`id` int(11) NOT NULL auto_increment,
`fullname` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
Dumping data for table `employee`
INSERT INTO `employee` (`id`, `fullname`, `address`, `city`, `state`) VALUES
(1, 'Raj', 'Inorbit Mall,Vashi', 'Pune', 'Maharastra'),
(2, 'Ravi', 'SV Road, Andheri (W)', 'Mumbai', 'Maharastra'),
(3, 'Barry', 'Krushal Commercial Complex,G.M. Road', 'Noida', 'UP'),
(4, 'Harry', 'Suburbia Mall, Linking Road, Bandra (W)', 'Delhi', 'Delhi'),
(5, 'Ian', 'Naman Plaza,S.V. Road, Opposite Shankar Lane', 'Nasik', 'Maharastra'),
(6, 'Sunny', 'Dynamix Mall, Y.J. Reality Ltd, Sant Dhyaneshwar Marg'
Code part
config.php
<?php
$host="localhost";
$username="root";
$password="";
$dbname="Work";
$con=mysql_connect("$host","$username","$password");
mysql_select_db("$dbname",$con);
?>
search.php
<?php
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Search page in php</title>
</head>
<body>
<form method="post" id="frm" name="frm" action="search.php">
<table width="500" border="0">
<tr>
<td>Search page in php</td>
<td>
<input type="text" name="fullname" id="fullname"/> </td>
</tr>
<tr>
<td>
<input type="submit" name="Submit" value="Search" /> </td>
<td> </td>
</tr>
</table>
</form>
<?php
if($_REQUEST["Submit"]=="Search")
{
$sql="select * from employee where fullname like '%$_REQUEST[fullname]%' OR address like '%$_REQUEST[fullname]%' OR city like '%$_REQUEST[fullname]%' OR state like '%$_REQUEST[fullname]%' OR id='$_REQUEST[fullname]'";
$sql_row=mysql_query($sql);
$num=@mysql_num_rows($sql_row);
if($num > 0)
{
?>
<table width="500" border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>
<td>City</td>
<td>State</td>
</tr>
<?php
while($sql_res=@mysql_fetch_assoc($sql_row))
{
?>
<tr>
<td><?php echo $sql_res["id"]; ?></td>
<td><?php echo $sql_res["fullname"]; ?></td>
<td><?php echo $sql_res["address"]; ?></td>
<td><?php echo $sql_res["city"]; ?></td>
<td><?php echo $sql_res["state"]; ?></td>
</tr>
<?php
} ?>
</table>
<?php
}
else
{
?>
<p>No Results found</p>
<?php
}
?>
<?php
}
?>
</body>
</html>Output


Showing records for Id, 1

Showing records for Id,2
Showing records for State, Orissa

Showing records for City, Noida
Showing records for Name, Ian
Showing records for Address, Suburbia
Showing no records for id, 7


reza parmarPosted Feb 21, 2014, 7:12 AM
Is the simultaneous combination of multiple items to be Jstjvastfadh, if possible, give an example. Thank you
shahzad mahmoodPosted Aug 24, 2013, 7:24 AM
reply sir
shahzad mahmoodPosted Aug 23, 2013, 5:47 PM
salaaam i want to add search page into online web site that search the products by name and category
np baijuPosted Apr 22, 2013, 10:40 PM
Thanks for your project.. One mistake is there in table. Once more very very thanks.
mani maranPosted Dec 11, 2012, 11:51 PM
can u display images from database to web pages using search function..? i will try to display images but it will display in encrypt format..can any one help me..thanks in advance.. -manish-
Husain AhmedPosted Jun 22, 2012, 7:57 AM
hello dear sir i am fan of your website bcz this is the best processec of tutorial have to you plz say how to create auto genrated password in php.. i am waiting for your answer
Husain AhmedPosted Jun 22, 2012, 7:53 AM
hi sir how to create auto genrated password
Vineet Kumar SainiPosted May 24, 2012, 1:00 AM
Good Nayak ji. It is very helpful for searching options in any project of PHP.
Frankie WilliamsPosted May 24, 2012, 12:24 AM
It's very helpful..
Aditya GaurPosted May 24, 2012, 12:05 AM
Hello Satyapriya, how to apply Intellisense ability to the search Textbox?
Satyapriya NayakPosted May 23, 2012, 11:21 PM
Hi Vipender, Refer the below link for validation in PHP http://www.c-sharpcorner.com/UploadFile/051e29/validation-in-php/
Vipendra VermaPosted May 22, 2012, 6:51 PM
I want to know how to create validation in PHP.
Amulya DehuryPosted May 22, 2012, 4:37 AM
Nice!