ARTICLE

Creating a Search Page in PHP

Posted by Satyapriya Nayak Articles | PHP May 21, 2012
In this article we will learn how to make a search page in PHP.
Reader Level:
Download Files:
 

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>&nbsp;</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

1.jpg

2.jpg

Showing records for Id, 1

3.jpg

Showing records for Id,2

4.jpg
Showing records for State, Orissa

5.jpg

Showing records for City, Noida

6.jpg
Showing records for Name, Ian


7.jpg

Showing records for Address, Suburbia


8.jpg

Showing no records for id, 7

9.jpg

Login to add your contents and source code to this article
post comment
     

Thanks for your project.. One mistake is there in table. Once more very very thanks.

Posted by np baiju Apr 22, 2013

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-

Posted by mani maran Dec 11, 2012

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

Posted by husain ahmed Jun 22, 2012

hi sir how to create auto genrated password

Posted by husain ahmed Jun 22, 2012

Good Nayak ji. It is very helpful for searching options in any project of PHP.

Posted by Vineet Kumar Saini May 24, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter