PHP SQL Search

PHP SQL Search is used to return the records details from the table based on the search condition.

PHP SQL Search


PHP SQL Search is used to return the records details from the table based on the search condition. The PHP SQL Search allows the user to put his search text  and choose a option from select box. The click on search button helps the user to show the records details based on search text put by the user.

Understand with Example 

This example illustrates how to create a search application in the PHP. To access the records from table 'user' in the database we create a table user. The select statement fetches the records from table 'user'.

Table: user

In this example we create a text box where user put his/her search term and choose a option from select box. When user submit his/her query then we select all list from database table where the query stream involve.

$find = strtoupper($find): The PHP strtoupper ( ) Function convert the search text into upper case.

$find = strip_tags($find) : The string functions is used to manipulate strings.

$find = trim ($find) : The PHP trim() function returns a string without leading and trailing spaces.

Source Code of searchForm.php 

<h2>Search Form</h2>

<form name="search" method="post" action="<?=$PHP_SELF?>">
  Seach for: <input type="text" name="find" /> in
  <Select NAME="field">
  <Option VALUE="fname">First Name</option>
  <Option VALUE="lname">Last Name</option>
  <Option VALUE="designation">Designation</option>

  <input type="hidden" name="searching" value="yes" />
  <input type="submit" name="search" value="Search" />

  if ($searching =="yes"){
  echo "<h3>Search Results</h3><p>";
  if ($find == ""){
  echo "<p>Please Enter a search term";
  mysql_connect("localhost", "root", "root") or die(mysql_error());
  mysql_select_db("test") or die(mysql_error());
  $find = strtoupper($find);
  $find = strip_tags($find);
  $find = trim ($find);

  $query = mysql_query("SELECT * FROM user WHERE upper($field) LIKE'%$find%'");
  while($result = mysql_fetch_array($query)){
  echo $result['fname'];
  echo " ";
  echo $result['lname'];
  echo "<br>";
  echo $result['designation'];
  echo "<br>";
  echo "<br>";

  if ($matches == 0){
  echo "Sorry, we can not find an entry to match your query<br><br>";

  echo "<b>Searched For:</b> " .$find;

Download Source Code


Share on Google+Share on Google+

PHP SQL Search

Posted on: April 3, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: PHP SQL Search   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 3, 2013
php script

Hi, Thank you for the code, it works perfect but just wondering how can i pipe out the results onto a new page rather than on the page itself cos i am using this script as part of a page to do the searching and i want the results to show up on a new page
February 20, 2013
opinion minning and sentiment analysis

hi am doing my academic project,please giude me how to match string of sentences(which are tokenized) with row of lexicons in mysql table
May 17, 2012
you can use to display

<?php // Initialize variables $db = "STUDENT"; $table = "student_list"; $field1 = "name"; $field2 = "entrydate"; $field3 = "graddate"; $field4 = "nationality"; $field5 = "average"; $tag = $_POST['name']; $grade = $_POST['grades']; $dbLink = new mysqli("localhost", "root", null, "student"); if(mysqli_connect_errno()) { die("MySQL connection failed: ". mysqli_connect_error()); } $mark1 = 0; $mark2 = 0; if($grade == 'A'){ $mark1 = 80; $mark2 = 90; } elseif($grade == 'B'){ $mark1 = 70; $mark2 = 79; } elseif($grade == 'C'){ $mark1 = 60; $mark2 = 69; } elseif($grade == 'D'){ $mark1 = 50; $mark2 = 59; } else{ $mark1 = 0; $mark2 = 100; } $sql = "SELECT * FROM ".$table." WHERE ".$field1." LIKE '%".$tag."%' AND ".$field5." BETWEEN '".$mark1."' AND '".$mark2."' "; $result = $dbLink->query($sql); if($result) { // Make sure there are some files in there if($result->num_rows == 0) { echo '<p>There are no files in the database</p>'; } else { // Print the top of a table echo '<table width="100%" border="1"> <tr> <td><b>Name</b></td> <td><b>Entry Date</b></td> <td><b>Graduation Date</b></td> <td><b>Nationality</b></td> <td><b>Average</b></td> </tr>'; // Print each file while($row = $result->fetch_assoc()) { echo " <tr> <td>{$row['name']}</td> <td>{$row['entrydate']}</td> <td>{$row['graddate']}</td> <td>{$row['nationality']}</td> <td>{$row['average']}</td> </tr>"; } // Close table echo '</table>'; } // Free the result $result->free(); } else { echo 'Error! SQL query failed:'; echo "<pre>{$dbLink->error}</pre>"; } // Close the mysql connection $dbLink->close(); ?>
July 15, 2012

thank you .
July 31, 2012
PHP search

Getting this Notice: Undefined variable: searching in H:\xampp\htdocs\searchSelect.php on line 16 I have changed the php file name to "searchSelect.php" from "searchForm.php"
October 20, 2012
Not working

this script is not working why? Please help me <h2>Search Form</h2> <form name="search" method="post" action="<?=$PHP_SELF?>"> Seach for: <input type="text" name="find" /> in <Select NAME="field"> <Option VALUE="fname">First Name</option> <Option VALUE="lname">Last Name</option> <Option VALUE="designation">Designation</option> </Select> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </form> <?php if ($searching =="yes"){ echo "<h3>Search Results</h3><p>"; if ($find == ""){ echo "<p>Please Enter a search term"; exit; } mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("membersystembasic") or die(mysql_error()); $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); $query = mysql_query("SELECT * FROM test WHERE upper($field) LIKE'%$find%'"); while($result = mysql_fetch_array($query)){ echo $result['fname']; echo " "; echo $result['lname']; echo "<br>"; echo $result['designation']; echo "<br>"; echo "<br>"; } $matches=mysql_num_rows($query); if ($matches == 0){ echo "Sorry, we can not find an entry to match your query<br><br>"; } echo "<b>Searched For:</b> " .$find; } ?>