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,
    <?php
    $host = "localhost";
    $user = "root";
    $pass = "";
    $dbName = "db_tutorial";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    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,
    <html>
    <head>
    <title> Halaman Pencarian </title>
    </head>
    <body>
    <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>
    </td>
    </tr>
    <tr> <td>  Name </td>
    <td> <input type="text" name="name"> </td>
    </tr>
    <td></td>
    <td> <input type="SUBMIT" name="SUBMIT" id="SUBMIT" value="search" > </td>
    </table>
    </form>
    </body>
    </html>
    
  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,
    <?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>No</td>
    <td>Nama Mahasiswa</td>
    <td>Alamat</td>
    </tr>";
    while ($data = mysql_fetch_array($result)) {  //fetch the result from query into an array
    echo "
    <tr>
    <td>".$data['no']."</td>
    <td>".$data['name']."</td>
    <td>".$data['address']."</td>
    </tr>";
    }
    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 :D Happy Coding..
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

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 lo

Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: