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
- Create database named db_tutorial
- Create table named tb_student, the structure is as shown below,
- Insert some sample data, just like in this picture.
- Done with the database!
Step 2 : Prepare the Work Folder
- Create a folder with the name searching in your document root
- Save all this file in this tutorial in this folder.
Step 3 : Create script to connect to DB
- Type the following script,
[sourcecode language=”php”]
<?php
$host = "localhost";
$user = "root";
$pass = "";
$dbName = "db_tutorial";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>
[/sourcecode] - 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.
Save with the name connect.php
Step 4 : Create a Searching Form
- Type the following script,
[sourcecode language=”php”]
<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>
[/sourcecode] - Save with the name formsearching.php
- 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
- Type the following script,
[sourcecode language=”php”]
<?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>";
?>
[/sourcecode] - Save with the name search_exe.php
- The explanation can be found at comment script
Step 6 : Testing Code
- Go to http://localhost/searching/formsearching.php. You can see the form like the picture below,
- Enter the name of the field name (eg name that has diinsert to the DB, namely june )
- Click the search button and you will see results,
Be creative with PHP 😀 Happy Coding..