(PHP Koneksi MYSQL)

Click here to load reader

download (PHP Koneksi MYSQL)

of 56

  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    38
  • download

    2

Embed Size (px)

description

PHP DASAR

Transcript of (PHP Koneksi MYSQL)

  • Konektivitas PHP - MySQLPHP - 2Ir. H. Sumijan, M.Sc

  • Koneksi PHP - MySQLUntuk 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 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 InputKoneksi PHP - MySQL

  • Champion

    Koneksi PHP - MySQL

  • 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

  • Simpan Liga

    Data Barang

    Skrip Simpan DataKoneksi PHP - MySQL

  • Koneksi PHP - MySQL

  • Kode Negara Champion

    Skrip Menampilkan DataKoneksi PHP - MySQL

  • Koneksi PHP - MySQL

  • Form PencarianBuat Input dengan type radio untuk memilih field pencarian.Buat Input dengan type text sebagai kata kunci pencarian.Buat tombol Cari, Input -> type SubmitKoneksi PHP - MySQL

  • Cari Database

    Searching Liga untuk database MySQL

    KodeNegara

    Masukkan kata yang anda cari

    Form - SearchKoneksi PHP - MySQL

  • Koneksi PHP - MySQL

  • Koneksi PHP - MySQL

  • Contoh Kasus - 1CREATE 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 : dbworkScript Table mahasiswa

  • Nama File : Config.php
  • Nama File : View.php

    View Records

  • Lanjutan Nama File : View.phpwhile($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

    Insert Records

    Insert Records

  • Lanjutan Nama File : Insert.php

    Name*

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

    Edit Records

  • Lanjutan Nama File : Edit.php

    Name*

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

  • Nama File : style.cssbody { 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 '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 : dbmhsScript Table mahasiswaContoh Kasus - 2

  • ?>php

  • NIM

  • ">Tanggal Lahir
  • Nama File : menu.php

  • NIM

    Nama

    Tanggal Lahir

    Kelas

    Jurusan

    Nama File : addmhs.php

  • BackListmhs.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 ('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

  • Nomor BP :

  • Nomor BP :

  • 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++;}?>

  • DAFTAR NILAI MAHASISWA
  • { $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

    ********************************************************