Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Konektivitas PHP - MySQL Konektivitas PHP - MySQL PHP - 2 Ir. H. Sumijan, M.Sc.

Presentasi serupa


Presentasi berjudul: "Konektivitas PHP - MySQL Konektivitas PHP - MySQL PHP - 2 Ir. H. Sumijan, M.Sc."— Transcript presentasi:

1 Konektivitas PHP - MySQL Konektivitas PHP - MySQL PHP - 2 Ir. H. Sumijan, M.Sc

2 Koneksi PHP - MySQL Untuk melakukan koneksi PHP dengan MySQL digunakan perintah: mysql_connect() -> skrip PHP. Syntak: mysql_connect(“hostname”,”username”,”password”);

3 Setelah terkoneksi, pilihlah database. Bila database yang diinginkan belum tersedia, buatlah databasenya. Buatlah tabel, dengan struktur datanya. Koneksi PHP - MySQL

4 <? //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

5 <? //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

6 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

7 Formulir Input Liga Masukkan Data Liga Kode Negara Skrip – Form Input Koneksi PHP - MySQL

8 Champion Koneksi PHP - MySQL

9

10 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

11 <? $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

12 <? $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

13

14 <? $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

15

16 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

17 Cari Database Searching Liga untuk database MySQL Kode Negara Masukkan kata yang anda cari Form - Search Koneksi PHP - MySQL

18

19 <? $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

20

21 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 9811 109 ST NW', 'London'), (5, 'Danny', 'Suite 303, 13220 St. Albert Trail', 'Sydney'), (6, 'Yettri', '9700 Jasper Avenue', 'Perth'); Nama Database : dbwork Script – Table mahasiswa

22 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()); ?>

23 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 ";

24 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

25 Nama File : Insert.php <?php function valid($name, $address,$city, $error) { ?> Insert Records <?php if ($error != '') { echo ' '.$error.' '; } ?> Insert Records

26 Lanjutan Nama File : Insert.php Name * " /> Address * " /> City * " />

27 <?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

28 $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

29 Nama File : Edit.php <?php function valid($id, $name, $address,$city, $error) { ?> Edit Records <?php if ($error != '') { echo ' '.$error.' '; } ?> "/> Edit Records

30 Lanjutan Nama File : Edit.php Name * " /> Address * " /> City * " /> <?php

31 } 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

32 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

33 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"); } ?>

34 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; }

35 CREATE TABLE mhs ( NIM varchar(16) NOT NULL default '', Nama varchar(145) NOT NULL default '', TglLahir date NOT NULL default '0000-00-00', Kelas varchar(15) NOT NULL default '', Jrs char(3) NOT NULL default '', PRIMARY KEY (NIM), KEY NIM (NIM) ) TYPE=MyISAM; INSERT INTO mhs VALUES ('091011152620001', 'Pradani', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620002', 'Atika', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620003', 'Danny', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620004', 'Debie', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620005', 'Cintya', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620006', 'Asep', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620007', 'Joko', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620008', 'Acong', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620009', 'Ucok', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620010', 'Baba', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620011', 'Khan', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620012', 'Nere', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620013', 'Sania', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620014', 'Riani', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620015', 'Ryan', '1966-07-10', 'SK-4', '262'); INSERT INTO mhs VALUES ('091011152620016', 'Wilujeng', '1966-07-10', 'SK-4', '262'); Nama Database : dbmhs Script – Table mahasiswa Contoh Kasus - 2

36 ?>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

37 <?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

38 NIM "> Nama "> Tanggal Lahir "> Kelas "> Jurusan "> Lanjutan Nama File : editmhs.php

39 <? 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

40 <?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

41 NIM "> Nama "> Tanggal Lahir "> Kelas "> Jurusan "> Lanjutan Nama File : deletemhs.php

42 <? 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

43 <?php echo " Add Mhs "; echo " Browse Mhs "; echo " List Mhs "; echo " Exit "; echo " "; ?> Nama File : menu.php

44 <?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

45 <? $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

46 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

47 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 ('091011152620003', 'KKKI12001', 'A'); INSERT INTO ambilmk VALUES ('091011152620003', 'KKKI12002', 'A'); INSERT INTO ambilmk VALUES ('091011152620003', 'KKKI12003', 'B'); INSERT INTO ambilmk VALUES ('091011152620003', 'KKKI12004', 'A'); INSERT INTO ambilmk VALUES ('091011152620003', 'KKKI12005', 'C'); INSERT INTO ambilmk VALUES ('091011152620016', 'KKKI12002', 'A'); INSERT INTO ambilmk VALUES ('091011152620016', 'KKKI12003', 'B'); INSERT INTO ambilmk VALUES ('091011152620016', 'KKKI12005', 'A'); Script – Table ambilmk

48 <? $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 "; } ?>

49 <?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

50 <?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

51 <?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

52 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++; } ?> " />

53 <? 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

54 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='091011152620001'; $bb=0;$tbb=0;$tsks=0;$nu=0; echo " "; while ($row=mysql_fetch_array($hasil)) Nama File : ListNilai.php

55 { $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

56 $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

57 Nama File :.php

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102


Download ppt "Konektivitas PHP - MySQL Konektivitas PHP - MySQL PHP - 2 Ir. H. Sumijan, M.Sc."

Presentasi serupa


Iklan oleh Google