Struktur tabel
-- Struktur dari tabel `anggota`
--
CREATE TABLE `anggota` (
`user_id` int(4) NOT NULL auto_increment,
`nama` varchar(20) NOT NULL,
`email` varchar(100) NOT NULL,
`alamat` varchar(100) NOT NULL,
`agama` varchar(15) NOT NULL,
`no_hp` bigint(14) NOT NULL,
`status` varchar(1) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
koneksi.php sesuaikan dengan variabel database anda
<?php
//variabel database
$nama_host="localhost";
$user_db="root";
$password_db="root";
$nama_db="nama database anda";
$entries=3;
//koneksi database
$koneksi=mysql_connect($nama_host,$user_db,$password_db);
mysql_select_db($nama_db,$koneksi);
date_default_timezone_set('Asia/Jakarta');
$tanggal=date("Y-m-d H:i:s");
//bila terkoneksi
if($koneksi){
//pilih database
mysql_select_db($nama_db);
}else{
echo "Database tidak terkoneksi";
}
?>
membuat form pencarian
formcari.php
<form name="formcari" method="post" action="cari.php">
<table width="330" border="0" align="center" cellpadding="0">
<tr bgcolor="orange">
<td height="25" colspan="3">
<strong> Cari Nama Anggota </strong>
</td>
</tr>
<tr> <td> Nama </td>
<td> <input type="text" name="nama"> </td>
</tr>
<td></td>
<td> <input type="SUBMIT" name="SUBMIT" id="SUBMIT" value="search" > </td>
</table>
</form>
membuat cari.php
<?php
include "koneksi.php";
$name= $_POST['nama'];
$q = "SELECT * from anggota where nama like '%$name%' ";
$result = mysql_query($q); //execute the query $q
echo "<center>";
echo "<h4> Hasil cari </h4>";
echo "<table border='1' cellpadding='5' cellspacing='8'>";
echo "
<tr bgcolor='orange'>
<td>Nama anggota</td>
<td>Alamat</td>
<td>NO HP</td>
</tr>";
while ($data = mysql_fetch_array($result)) {
echo "
<tr>
<td>".$data['nama']."</td>
<td>".$data['alamat']."</td>
<td>".$data['no_hp']."</td>
</tr>";
}
echo "</table>";
?>
Demikianlah contoh Pencarian Data dengan PHP pada Database mysql
No comments:
Post a Comment