SQL & PHP Tutorial

search data in database using php


In this tutorial you'll learn how to search a SQL database using PHP.

SQL tutorial



Click here to insert data using Forms

searching sql database


I’m going to create 2 files
File 1: search-record.htm
Let's create an HTML form that can be used to search records into the database

		  
< html> < head> < Title>Search Record< /Title> < head> < body> < Form>

We also need to make sure that the form method attribute is “post”
so as to access the data being entered in a reliable way in the next page being directed “retrieve-data.php”
so that the data being entered in the textboxes can then be retrieved data from the database in the “retrieve-data.php” page.

< form action="retrieve-data.php" method="Post">
< input type=“text” name=“ProductName” id=“ProductName”>

The < input> element is the most important form element. defines a one-line input field for text input:

< input type=“text” name=“ProductName” id=“ProductName”>

< input type="submit" value="Search">

Note: The form itself is not visible you can test it now. But I’m going to add some description

< form action="retrieve-data.php" method="Post">

Product_Name: 
< input type=“text” name=“ProductName” id=“ProductName”>

< < input type="submit" value="Search"> < /form>

Execute the file 1: search-record.htm

search-record
	  

This is how the HTML code above will be displayed in a browser.
Our HTML Form file is complete, now I want to complete PHP file

< /form> < /body> < /html>

Creating File 2:
retrieve-data.php

< ?php>
$ProductName = $_POST['ProductName'];
? >

we are geting ProductName from HTML FORM using php script.

search-record-by-product-name

//Connect to the MySql database


   $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
		
   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));

Execute the following Query statement to Search data from the Product Table

  $query = mysql_query("SELECT * FROM Product WHERE (`ProductName` LIKE '%".$ProductName."%') ");


Check if Product Name is Exist in the table, show the result
Else
Product not found

    if(mysql_num_rows($query) > 0){
	   echo "Record found ";
       while($results = mysql_fetch_array($query)){ 
	
    echo "(".$results['ProductID'].",".$results['ProductName'].",“ .$results['Price'].",".$results['ProductDescription'].")";
    } 
	}
    else
	   {echo “Result not found”;}


Type product name in Search text Box

search-textbox

Click Search Button
after clicking the Search button data processing start like this
When the user fills out the form above and clicks the search button, the form data is sent for processing to a PHP file named "retrieve-data.php". The form data is sent with the HTTP POST method.
After Executing the following statement to search Data from the Table Variable $ProductName gets value Corola, then search the table "Product" using SQL query
"$query = mysql_query("SELECT * FROM Product WHERE (`ProductName` LIKE '%".$ProductName."%') ");"

data-process-html-form-to-php

search-record-process

if the record found show the result
Record found
(111,Corola,2000$,Made in Japan)

record-found.png

Congratulation its Working 100%

Writing all php code together

  < ?php>

  $ProductName = $_POST['ProductName'];


   $cn = mysql_connect('localhost', ‘client', ‘Client123') or
        die('Unable to connect to server');
		
   mysql_select_db("shaigle_test", $cn) or	
      die(mysql_error($cn));
	  
	$query = mysql_query("SELECT * FROM Product WHERE (`ProductName` LIKE '%".$ProductName."%') ");
	
if(mysql_num_rows($query) > 0){
       echo "Record found ";
       while($results = mysql_fetch_array($query)){ 
	
    echo "(".$results['ProductID'].",".$results['ProductName'].",“ .$results['Price'].",".$results['ProductDescription'].")";
    } 
	}
    else
	   {echo “Result not found”;}
	   
	mysql_close($cn);
	   
 ? >