Konektivitas PHP - MySQL Konektivitas PHP - MySQL PHP - 2 Ir. H. Sumijan, M.Sc
Koneksi PHP - MySQL Untuk melakukan koneksi PHP dengan MySQL digunakan perintah: mysql_connect() -> skrip PHP. Syntak: mysql_connect(“hostname”,”username”,”password”);
Setelah terkoneksi, pilihlah database. Bila database yang diinginkan belum tersedia, buatlah databasenya. Buatlah tabel, dengan struktur datanya. Koneksi PHP - MySQL
<? //Membuat database MySQL $nama_db = “dbbola"; $buat_db = "create database $nama_db"; $sambung = mysql_connect("localhost","root",""); if($sambung){ echo "Koneksi Berhasil";} else { echo "Koneksi Gagal";} $q_db = mysql_query($buat_db); if($q_db){ echo " Database $nama_db berhasil dibuat";} else { echo " Database $nama_db gagal dibuat";} ?> Koneksi Database Koneksi PHP - MySQL
<? //Membuat tabel MySQL $nama_db = “dbbola"; $nama_tbl = "Liga"; $sambung = mysql_connect("localhost","root",""); if($sambung){ echo "Koneksi Berhasil";} else { echo "Koneksi Gagal";} 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 " Tabel$nama_tbl berhasil dibuat";} else { echo " Tabel $nama_tbl gagal dibuat";} ?> Membuat Tabel Koneksi PHP - MySQL
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. Koneksi PHP - MySQL
Formulir Input Liga Masukkan Data Liga Kode Negara Skrip – Form Input Koneksi PHP - MySQL
Champion Koneksi PHP - MySQL
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”); Koneksi PHP - MySQL
<? $sambung = mysql_connect("localhost","root",""); mysql_select_db(dbbola,$sambung) or die("Koneksi ke dbbola gagal"); ?> Simpan Liga Data Barang <? $kode = $_POST[kode]; $negara = $_POST[negara]; $champion = $_POST[champion]; ?> Skrip – Simpan Data Koneksi PHP - MySQL
<? $q_simpan = "insert into Liga values('$kode','$negara','$champion')"; $hasil = mysql_query($q_simpan,$sambung); if($hasil){ echo "Data tersimpan: "; echo "Kode : ".$kode." "; echo "Negara : ".$negara." "; echo "Champion : ".$champion." "; }else{ echo "Data gagal disimpan";} ?> Skrip – Simpan Data Koneksi PHP - MySQL
<? $tampil = "select * from Liga"; $q_buat = mysql_query($tampil,$sambung); ?> Kode Negara Champion <? while($row = mysql_fetch_row($q_buat)){ echo (" $row[0] "); echo (" $row[1] "); echo (" $row[2] "); } ?> <? // ?> Skrip – Menampilkan Data Koneksi PHP - MySQL
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 Koneksi PHP - MySQL
Cari Database Searching Liga untuk database MySQL Kode Negara Masukkan kata yang anda cari Form - Search Koneksi PHP - MySQL
<? $kolom=$_POST['kolom']; $cari=$_POST['cari']; $conn=mysql_connect("localhost","root",""); mysql_select_db(“dbbola", $conn); $hasil=mysql_query("select * from Liga where $kolom like '%$cari%'", $conn); $jumlah=mysql_num_rows($hasil); echo " "; echo "Ditemukan: $jumlah"; echo " "; while($baris=mysql_fetch_array($hasil)) { echo "Kode : "; echo $baris[0]; echo " "; echo "Negara : "; echo $baris[1]; echo " "; echo "Champion :"; echo $baris[2]; } ?> Skrip Search Koneksi PHP - MySQL
Contoh Kasus - 1 CREATE TABLE `employee` ( `id` int(12) NOT NULL auto_increment, `name` varchar(255) NOT NULL, `address` varchar(255) NOT NULL, `city` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; INSERT INTO `employee` (`id`, `name`, `address`, `city`) VALUES (1, 'Soe', '10 street dane', 'Pune'), (2, 'Debie', '12907A 53 St NW', 'Mumbai'), (3, 'Atika', '3rd Floor, 888 Fort Street', 'Noida'), (4, 'Widya', 'Sir Frederick W Haultain Building ST NW', 'London'), (5, 'Danny', 'Suite 303, St. Albert Trail', 'Sydney'), (6, 'Yettri', '9700 Jasper Avenue', 'Perth'); Nama Database : dbwork Script – Table mahasiswa
Nama File : Config.php <? $sDbHost = 'localhost'; $sDbName = ‘dbwork'; $sDbUser = 'root'; $sDbPwd = ''; $dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. '. mysql_error()); mysql_select_db($sDbName,$dbConn) or die('Cannot select database. '. mysql_error()); ?>
Nama File : View.php View Records <?php include('config.php'); $result = mysql_query("SELECT * FROM employee") or die(mysql_error()); echo " "; echo " Id Name Address City Edit Delete ";
Lanjutan Nama File : View.php while($row = mysql_fetch_array( $result )) { echo " "; echo ' '. $row['id']. ' '; echo ' '. $row['name']. ' '; echo ' '. $row['address']. ' '; echo ' '. $row['city']. ' '; echo ' Edit '; echo ' Delete '; echo " "; } echo " "; ?> Insert new record
Nama File : Insert.php <?php function valid($name, $address,$city, $error) { ?> Insert Records <?php if ($error != '') { echo ' '.$error.' '; } ?> Insert Records
Lanjutan Nama File : Insert.php Name * " /> Address * " /> City * " />
<?php } include('config.php'); if (isset($_POST['submit'])) { $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); $address = mysql_real_escape_string(htmlspecialchars($_POST['address'])); $city = mysql_real_escape_string(htmlspecialchars($_POST['city'])); if ($name == '' || $address == '' || $city == '') { Lanjutan Nama File : Insert.php
$error = 'Please enter the details!'; valid($name, $address, $city,$error); } else { mysql_query("INSERT employee SET name='$name', address='$address', city='$city'") or die(mysql_error()); header("Location: view.php"); } else { valid('','','',''); } ?> Lanjutan Nama File : Insert.php
Nama File : Edit.php <?php function valid($id, $name, $address,$city, $error) { ?> Edit Records <?php if ($error != '') { echo ' '.$error.' '; } ?> "/> Edit Records
Lanjutan Nama File : Edit.php Name * " /> Address * " /> City * " /> <?php
} include('config.php'); if (isset($_POST['submit'])) { if (is_numeric($_POST['id'])) { $id = $_POST['id']; $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); $address = mysql_real_escape_string(htmlspecialchars($_POST['address'])); $city = mysql_real_escape_string(htmlspecialchars($_POST['city'])); if ($name == '' || $address == '' || $city == '') { $error = 'ERROR: Please fill in all required fields!'; valid($id, $name, $address,$city, $error); } else { mysql_query("UPDATE employee SET name='$name', address='$address',city='$city' WHERE id='$id'") or die(mysql_error()); header("Location: view.php"); } else { echo 'Error!'; } Lanjutan Nama File : Edit.php
else { if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { $id = $_GET['id']; $result = mysql_query("SELECT * FROM employee WHERE id=$id") or die(mysql_error()); $row = mysql_fetch_array($result); if($row) { $name = $row['name']; $address = $row['address']; $city = $row['city']; valid($id, $name, $address,$city,''); } else { echo "No results!"; } } else { echo 'Error!'; } } ?> Lanjutan Nama File : Edit.php
Nama File : Delete.php <?php include('config.php'); if (isset($_GET['id']) && is_numeric($_GET['id'])) { $id = $_GET['id']; $result = mysql_query("DELETE FROM employee WHERE id=$id") or die(mysql_error()); header("Location: view.php"); } else { header("Location: view.php"); } ?>
Nama File : style.css body { color:#2e6ab1; } table { border-collapse: collapse; } th { background-color: #2e6ab1; padding-left: 14px; padding-right: 8px; border: 1px solid #969BA5; text-align:left; color:#ffffff; } td { background-color: #F0F0F0; padding-left: 8px; padding-right: 8px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #969BA5; color:#2e6ab1; } Input, select { color:#2e6ab1; }
CREATE TABLE mhs ( NIM varchar(16) NOT NULL default '', Nama varchar(145) NOT NULL default '', TglLahir date NOT NULL default ' ', Kelas varchar(15) NOT NULL default '', Jrs char(3) NOT NULL default '', PRIMARY KEY (NIM), KEY NIM (NIM) ) TYPE=MyISAM; INSERT INTO mhs VALUES (' ', 'Pradani', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Atika', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Danny', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Debie', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Cintya', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Asep', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Joko', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Acong', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Ucok', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Baba', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Khan', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Nere', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Sania', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Riani', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Ryan', ' ', 'SK-4', '262'); INSERT INTO mhs VALUES (' ', 'Wilujeng', ' ', 'SK-4', '262'); Nama Database : dbmhs Script – Table mahasiswa Contoh Kasus - 2
?>php <? $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="Select * from mhs"; $hasil=mysql_query($sql); while ($row=mysql_fetch_array($hasil)) { echo "$row[NIM]-$row[Nama]-$row[TglLahir]- $row[Kelas]- $row[Jrs]"; echo " Edit | "; echo " Delete "; } ?> Nama File : BrowseMhs.php
<?php $nim=$_REQUEST[NIM]; $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="Select * from mhs where NIM='$nim' "; $hasil=mysql_query($sql); $row=mysql_fetch_array($hasil); $nama=$row[Nama]; $tgllahir=$row[TglLahir]; $kelas=$row[Kelas]; $jrs=$row[Jrs]; ?> Nama File : editmhs.php
NIM "> Nama "> Tanggal Lahir "> Kelas "> Jurusan "> Lanjutan Nama File : editmhs.php
<? if ($_REQUEST[simpan]=='Update') { echo "Proses Updating..!"; $nim=$_REQUEST[NIM];$nama=$_REQUEST[Nama]; $tgllahir=$_REQUEST[TglLahir];$kelas=$_REQUEST[Kelas]; $jrs=$_REQUEST[Jrs]; $sql="update mhs set NIM='$nim', Nama='$nama', TglLahir='$tgllahir', Kelas='$kelas', Jrs='$jrs' where NIM='$nim' "; $hasil=mysql_query($sql); echo "Berhasil di Update..!"; } ?> Back Lanjutan Nama File : editmhs.php
<?php $nim=$_REQUEST[NIM]; $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="Select * from mhs where NIM='$nim' "; $hasil=mysql_query($sql); $row=mysql_fetch_array($hasil); $nama=$row[Nama]; $tgllahir=$row[TglLahir]; $kelas=$row[Kelas]; $jrs=$row[Jrs]; ?> Nama File : deletemhs.php
NIM "> Nama "> Tanggal Lahir "> Kelas "> Jurusan "> Lanjutan Nama File : deletemhs.php
<? if ($_REQUEST[simpan]=='Delete') { echo "Proses Deleting..!"; $sql=" delete from mhs where NIM='$nim' "; $hasil=mysql_query($sql); echo "Berhasil di Hapus..!"; } ?> Back Lanjutan Nama File : deletemhs.php
<?php echo " Add Mhs "; echo " Browse Mhs "; echo " List Mhs "; echo " Exit "; echo " "; ?> Nama File : menu.php
<?php $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); ?> NIM Nama Tanggal Lahir Kelas Jurusan Nama File : addmhs.php <? if ($_REQUEST[simpan]=='Add') { echo "Proses Adding..!"; $nim=$_REQUEST[NIM];$nama=$_REQUEST[Nama]; $tgllahir=$_REQUEST[TglLahir];$kelas=$_REQUEST[Kelas]; $jrs=$_REQUEST[Jrs]; $sql="Insert into mhs values ('$nim', '$nama', '$tgllahir', '$kelas', '$jrs')"; $hasil=mysql_query($sql); echo "Berhasil di Disimpan..!"; } ?> Back
<? $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="Select * from mhs"; $hasil=mysql_query($sql); while ($row=mysql_fetch_array($hasil)) { echo "$row[NIM]-$row[Nama]-$row[TglLahir]- $row[Kelas]- $row[Jrs] "; } ?> Back Listmhs.php
CREATE TABLE mtk ( KodeMK varchar(10) NOT NULL default '', NamaMK varchar(145) NOT NULL default '', SKS int, KodeJrs char(3) NOT NULL default '', PRIMARY KEY (KodeMK), KEY KodeMK (KodeMK) ) TYPE=MyISAM; INSERT INTO mtk VALUES ('KKKI12001', 'Pancasila', '2', '263'); INSERT INTO mtk VALUES ('KKKI12002', 'Agama', '2', '263'); INSERT INTO mtk VALUES ('KKKI12003', 'Bahasa Indonesia', '2', '263'); INSERT INTO mtk VALUES ('KKKI12004', 'Kewarganegaraan', '2', '263'); INSERT INTO mtk VALUES ('KKKI12005', 'Etika Profesi', '2', '263'); Script – Table matakuliah
CREATE TABLE ambilmk ( nim varchar(16) NOT NULL default '', kodeMK varchar(10) NOT NULL default '', nilai char(2) defalt NULL, PRIMARY KEY (nim,kodeMK) ) TYPE=MyISAM; INSERT INTO ambilmk VALUES (' ', 'KKKI12001', 'A'); INSERT INTO ambilmk VALUES (' ', 'KKKI12002', 'A'); INSERT INTO ambilmk VALUES (' ', 'KKKI12003', 'B'); INSERT INTO ambilmk VALUES (' ', 'KKKI12004', 'A'); INSERT INTO ambilmk VALUES (' ', 'KKKI12005', 'C'); INSERT INTO ambilmk VALUES (' ', 'KKKI12002', 'A'); INSERT INTO ambilmk VALUES (' ', 'KKKI12003', 'B'); INSERT INTO ambilmk VALUES (' ', 'KKKI12005', 'A'); Script – Table ambilmk
<? $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="Select * from mtk"; $hasil=mysql_query($sql); while ($row=mysql_fetch_array($hasil)) { echo "$row[KodeMK]-$row[NamaMK]-$row[SKS]-$row[KodeJrs]"; echo " Edit | "; echo " Delete "; } ?>
<?php mysql_connect("localhost", "root", ""); mysql_select_db("dbmhs"); $sql="Select * from mhs"; $hasil=mysql_query($sql); ?> Nomor BP : <? while ($row=mysql_fetch_array($hasil)) { echo " $row[NIM] "; } echo " "; $query = "SELECT * FROM mtk"; $hasil = mysql_query($query); $no = 1; while ($data = mysql_fetch_array($hasil)) { echo " ".$data['KodeMK'].' - '.$data['NamaMK']." "; $no++; } ?> " /> Nama File : FormInputkrs.php
<?php mysql_connect("localhost", "root", ""); mysql_select_db("dbmhs"); $nim = $_REQUEST['nim']; $jumMK = $_POST['jumMK']; for($i = 1; $i <= $jumMK; $i++) { $mk = $_POST['mk'.$i]; if (!empty($mk)) { $query = "INSERT INTO ambilMK VALUES('$nim', '$mk', '-')"; mysql_query($query); } echo "Terimakasih sudah memilih matakuliah"; ?> Nama File : simpan.php
<?php header('Content-type:text/vnd.wap.wml'); echo(' '); mysql_connect("localhost", "root", ""); mysql_select_db("dbmhs"); $sql="Select * from mtk"; $hasil=mysql_query($sql); ?> Nomor BP : <? while ($row=mysql_fetch_array($hasil)) { echo " $row[KodeMK] "; } echo " "; $query = "SELECT * FROM ambilmk, mhs, mtk where ambilmk.nim=mhs.NIM and ambilmk.kodeMK=mtk.KodeMK order by mhs.NIM"; $hasil = mysql_query($query); $no = 1; FormNilai.php
while ($data = mysql_fetch_array($hasil)) { echo " "; echo " A "; echo " B "; echo " C "; echo " D "; echo " E "; echo " TL "; echo " "; echo " ".$data['nim'].' - '.$data['NamaMK'].' - '.$data['kodeMK']." "; $no++; } ?> " />
<? header('Content-type:text/vnd.wap.wml'); echo(' '); mysql_connect("localhost", "root", ""); mysql_select_db("dbmhs"); $jumMK = $_POST['jumMK']; for($i = 1; $i <= $jumMK; $i++) { $mk = $_REQUEST['mk'.$i]; $nim = $_REQUEST['nim'.$i]; $nl = $_REQUEST['nl'.$i]; if (!empty($mk)) { $query = "update ambilMK set nilai='$nl' where nim='$nim' and kodeMK='$mk'"; mysql_query($query); } echo "Terimakasih sudah memilih matakuliah"; ?> simpan1.php
DAFTAR NILAI MAHASISWA <? $con=mysql_connect("localhost","root",""); $db=mysql_select_db("dbmhs"); $sql="SELECT mhs.NIM, Nama, mtk.KodeMK, NamaMK, SKS, nilai FROM ambilmk, mhs, mtk where ambilmk.nim=mhs.NIM and ambilmk.kodeMK=mtk.KodeMK order by mhs.NIM"; $hasil=mysql_query($sql); $nim=' '; $bb=0;$tbb=0;$tsks=0;$nu=0; echo " "; while ($row=mysql_fetch_array($hasil)) Nama File : ListNilai.php
{ $nu++; if ($nim!=$row[NIM]) { if ($nu>1) { echo " Indek Prestasi :"; echo number_format($ip,2,',','.'); echo " "; $bb=0;$tbb=0;$tsks=0;$nu=0; } if ($row[nilai]=='A') $mutu=4; elseif ($row[nilai]=='B') $mutu=3; elseif ($row[nilai]=='C') $mutu=2; elseif ($row[nilai]=='D') $mutu=1; elseif ($row[nilai]=='E') $mutu=0; elseif ($row[nilai]=='TL‘) $mutu='0'; Lanjutan Nama File : ListNilai.php
$bb=$mutu*$row[SKS]; $tsks=$tsks+$row[SKS]; $tbb=$tbb+$bb; if ($tbb>0) $ip=$tbb/$tsks; echo "$row[NIM]-$row[Nama]-$row[KodeMK]-$row[NamaMK]- $row[SKS]-$row[nilai]-$mutu-$bb "; $nim=$row[NIM]; } echo " Indek Prestasi :"; echo number_format($ip,2,',','.'); echo " "; ?> Lanjutan Nama File : ListNilai.php
Nama File :.php