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