Konektivitas PHP - MySQL 1
Untuk melakukan koneksi PHP dengan MySQL digunakan perintah: mysql_connect() -> skrip PHP. Syntak: mysql_connect( hostname, username, password ); 2
Setelah terkoneksi, pilihlah database. Bila database yang diinginkan belum tersedia, buatlah databasenya. Buatlah tabel, dengan struktur datanya. 3
<? //Membuat database MySQL $nama_db = "faruq"; $buat_db = "create database $nama_db"; $sambung = mysql_connect("localhost","root",""); if($sambung){ echo "Koneksi Berhasil";} else { echo "Koneksi Gagal";} Koneksi Database $q_db = mysql_query($buat_db); if($q_db){ echo "<br> Database $nama_db berhasil dibuat";} else { echo "<br> Database $nama_db gagal dibuat";}?> 4
5
<? //Membuat tabel MySQL $nama_db = "faruq"; $nama_tbl = "Liga"; $sambung = mysql_connect("localhost","root",""); if($sambung){ echo "Koneksi Berhasil";} else { echo "Koneksi Gagal";} Membuat Tabel mysql_select_db($nama_db,$sambung) or die("koneksi ke $nama_db gagal"); $buat_tbl = "create table $nama_tbl (kode char(5) not null, negara char(10) not null, champion int)"; $qtbl = mysql_query($buat_tbl,$sambung); if($qtbl){ echo "<br>tabel$nama_tbl berhasil dibuat";} else { echo "<br>tabel $nama_tbl gagal dibuat";}?> 6
7
Membuat Form sebagai input pada sistem database. Input disimpan dalam satu tabel tertentu. Pada attribut action ditentukan file yang memiliki skrip PHP untuk menyimpan input ke tabel pada database. 8
<html> <head> <title>formulir Input Liga</title> <meta author=agus> </head> <body> <h1>masukkan Data Liga</h1> <form method=post action=simpan.php> <table> <tr> <td>kode</td> <td><input type=text name=kode size=20></td> </tr> <tr> <td>negara</td> <td><input type=text name=negara size=20></td> </tr> Skrip Form Input 9
<tr> <td>champion</td> <td><input type=text name=champion size=20></td> </tr> <tr> <td align=center><input type=submit name=submit value=simpan></td> <td align=center><input type=reset name=reset value=ulangi></td> </tr> </table> </form> </body> </html> 10
11
Untuk menyimpan input ke tabel MySQL, terlebih dahulu input dibaca sesuai metode yang digunakan. Untuk menyimpan ke database digunakan perintah: $hasil = mysql_query( SQL Statement ); 12
<? $sambung = mysql_connect("localhost","root",""); mysql_select_db(faruq,$sambung) or die("koneksi ke faruq gagal");?> <html> <head> <title>simpan Liga</title> <meta author=agus> </head> <body> <h1>data Barang</h1> <? $kode = $_POST[kode]; $negara = $_POST[negara]; $champion = $_POST[champion];?> Skrip Simpan Data 13
<? $q_simpan = "insert into Liga values('$kode','$negara','$champion')"; $hasil = mysql_query($q_simpan,$sambung); if($hasil){ echo "Data tersimpan: <br>"; echo "Kode : ".$kode."<br>"; echo "Negara : ".$negara."<br>"; echo "Champion : ".$champion."<br>"; }else{ echo "Data gagal disimpan";}?> <form method=post action=frm_input_liga.php> <input type=submit value=kembali> </form> </body> </html> Skrip Simpan Data 14
15
<? $tampil = "select * from Liga"; $q_buat = mysql_query($tampil,$sambung);?> <table border=1> <tr> <td>kode</td> <td>negara</td> <td>champion</td> </tr> <? while($row = mysql_fetch_row($q_buat)){ echo ("<tr align=center><td>$row[0]</td>"); echo ("<td>$row[1]</td>"); echo ("<td>$row[2]</td></tr>"); }?> </table> <? //<form method=post action=edit.php><input type=submit value=edit name=edit></form> //<form method=post action=tambah.php><input type=submit value=tambah name=tambah></form>?> Skrip Menampilkan Data 16
17
Form Pencarian Buat Input dengan type radio untuk memilih field pencarian. Buat Input dengan type text sebagai kata kunci pencarian. Buat tombol Cari, Input -> type Submit 18
<HTML> <HEAD> Form - Search <title>cari Database</title> </HEAD> <BODY> <h1>searching Liga untuk database MySQL</h1> <form action="hasilsearch.php" method="post"> <select name="kolom"> <option value="kode">kode</option> <option value="negara">negara</option> </select> Masukkan kata yang anda cari <input type ="text" type ="text" name="cari"> <input type ="submit" value="cari" > </form> </BODY> </HTML> 19
20
<? $kolom=$_post['kolom']; $cari=$_post['cari']; $conn=mysql_connect("localhost","root",""); mysql_select_db("faruq", $conn); $hasil=mysql_query("select * from Liga where $kolom like '%$cari%'", $conn); $jumlah=mysql_num_rows($hasil); echo "<br>"; echo "Ditemukan: $jumlah"; echo "<br>"; while($baris=mysql_fetch_array($hasil)) { echo "Kode : "; echo $baris[0]; echo "<br>"; echo "Negara : "; echo $baris[1]; echo "<br>"; echo "Champion :"; echo $baris[2]; }?> Skrip Search 21
22
Sekian - Terima Kasih 23