Tutorial Searching Data with PHP and Mysql

Case Study : Create script for searching data in the database
Requirements : Webserver Packages, already installed.

Searching, is a facility that is almost always present in every application, whether web based or desktop applications. Each web page / blog will also equipped with this facility. The following tutorial will provide the basic concept of searching, by doing a search on terstruktu data (database).

Ok, just straight practice.

Step 1 : Prepare the Database

  1. Create database named db_tutorial
  2. Create table named tb_student, the structure is as shown below,
  3. Insert some sample data, just like in this picture.
  4. Done with the database!

Step 2 : Prepare the Work Folder

  1. Create a folder with the name searching in your document root
  2. Save all this file in this tutorial in this folder.

Step 3 : Create script to connect to DB

  1. Type the following script,
    [sourcecode language=”php”]
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    or die ("Connect Failed !! : ".mysql_error());
  2. Save with the name connect.php

  3. Explanation: The script will be used for linking us with a database application db_tutorial. Customize your username and password from the settings of your mysql.

Step 4 : Create a Searching Form

  1. Type the following script,
    [sourcecode language=”php”]
    <title> Halaman Pencarian </title>
    <form name="formcari" method="post" action="search_exe.php">
    <table width="330" border="0" align="center" cellpadding="0">
    <tr bgcolor="orange">
    <td height="25" colspan="3">
    <strong> Student Searching ! </strong>
    <tr> <td> Name </td>
    <td> <input type="text" name="name"> </td>
    <td> <input type="SUBMIT" name="SUBMIT" id="SUBMIT" value="search" > </td>
  2. Save with the name formsearching.php
  3. Explanation: The script above is a normal HTML script that will display the form. See the part,
    action="search_exe.php", bagian ini dimaksudkan bahwa form akan diproses oleh script search_exe.php

Step 5 : Create a processing script and displays the results of searching

  1. Type the following script,
    [sourcecode language=”php”]
    include "connect.php";
    $name= $_POST[‘name’]; //get the nama value from form
    $q = "SELECT * from tb_student where name like ‘%$name%’ "; //query to get the search result
    $result = mysql_query($q); //execute the query $q
    echo "<center>";
    echo "<h2> Hasil Searching </h2>";
    echo "<table border=’1′ cellpadding=’5′ cellspacing=’8′>";
    echo "
    <tr bgcolor=’orange’>
    <td>Nama Mahasiswa</td>
    while ($data = mysql_fetch_array($result)) { //fetch the result from query into an array
    echo "
    echo "</table>";
  2. Save with the name search_exe.php
  3. The explanation can be found at comment script

Step 6 : Testing Code

  1. Go to http://localhost/searching/formsearching.php. You can see the form like the picture below,
  2. Enter the name of the field name (eg name that has diinsert to the DB, namely june )
  3. Click the search button and you will see results,

Be creative with PHP 😀 Happy Coding..

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.