Handout Pemrograman Web I PDF
Handout Pemrograman Web I PDF
Bab I
Pendahuluan PHP
Sebuah File PHP dapat terdiri dari teks, tag dan skrip HTML. Skrip dalam sebuah file PHP akan
di eksekusi di server.
Pengertian PHP
PHP merupakan singkatan dari PHP: Hypertext Preprocessor .
PHP merupakan server-side scripting language, seperti ASP.
Skrip PHP dieksekusi di server dan hasilnya dikirimkan ke client (browser).
PHP mendukung berbagai jenis database (contoh : MySQL, Informix, Oracle, Sybase,
Solid, PostgreSQL, Generic ODBC, dll.)
PHP merupakan perangkat lunak open source.
PHP dapat di-download dan digunakan secara gratis.
PHP + MySQL
PHP dikombinasikan dengan MySQL akan menghasilkan aplikasi yang cross-platform
(artinya kita dapat mengembangkan aplikasi di Windows dan dijalankan di server
Unix).
Mengapa PHP?
PHP dapat berjalan pada bermacam platforms (Windows, Linux, Unix, dll.).
PHP kompatibel dengan hampir semua server web yang ada (Apache, IIS, etc.).
PHP dapat di-download secara gratis dari situs resminya: http://www.php.net/
PHP mudah dipelajari dan berjalan secara efisien di server web.
1
Modul Pemrograman PHP & MySQL
Bab 2
PHP Installation
Jika web server mendukung PHP kita tidak perlu melakukan apapun!. kita tidak perlu
menkompilasi apapun atau menginstalasi tool-tool tambahan kita hanya perlu menulis file-
file *.php pada direktori web pada computer kita dan web server akan menerjemahkan file-
file tersebut dan dijalankan di browser.
Bagaimanapun juga, jika server yang kita miliki tidak mendukung PHP, maka kita harus
melakukan instalasi PHP. di bawah ini cara mengintalasi PHP.
situs : http://www.php.net/manual/en/install.php
Download PHP
situs : http://www.php.net/downloads.php
Download MySQL
situs : http://www.mysql.com/downloads/index.html
2
Modul Pemrograman PHP & MySQL
Bab 3
PHP Syntax
Kita tidak dapat melihat source code PHP pada browser - kita hanya akan melihat output dari
file PHP yaitu file HTML. Hal ini dikarenakan skrip PHP dieksekusi di server sebelum hasilnya
dikirimkan ke browser.
Skrip PHP dapat juga ditulis pada pasangan tag <? dan ?>.
<?php
?>
program3-1.php
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
program3-2.php.
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
3
Modul Pemrograman PHP & MySQL
Bab 4
Variabel
Variabel digunakan untuk menyimpan suatu nilai, seperti angka, string atau hasil dari suatu
fungsi, sehingga dapat digunakan berulangkali pada program.
Variabel di PHP
Semua variabel di PHP selalu diawali dengan symbol $. Variabel dapat terdiri dari string,
number, atau array.
Program4-1.php.
<html>
<body>
<?php
$txt="Hello World";
echo $txt;
?>
</body>
</html>
Untuk menggabungkan dua atau lebih variabel, digunakan operator titik (.)
Program4-2.php.
<html>
<body>
<?php
$txt1="Hello World";
$txt2="1234";
$Nilai1=1234;
$Nilai2=4321;
$Hasil=$Nilai1+Nilai2;
echo $txt1 . " " . $txt2 ;
echo $Hasil;
?>
</body>
</html>
Catatan.
Perintah-perintah PHP dapat disisipkan dibagian manapun dalam dokumen html
4
Modul Pemrograman PHP & MySQL
Bab 5
Operator PHP
Operator Aritmetika
Operator Keterangan Contoh Hasil
+ Penambahan x=2 4
x+2
- Pengurangan x=2 3
5-x
* Perkalian x=4 20
x*5
/ Pembagian 15/5 3
5/2 2.5
% Modulus (division remainder) 5%2 1
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--
5
Modul Pemrograman PHP & MySQL
Program5-1.php.
<html>
<title>Operator Penjumlahan</title>
<body>
<?php
$var1=10;
$var2=20;
print $var1 + $var2."<br>";
echo $var1 + $var2."<br>";
$Jum=$var1+$var2;
echo $Jum;
?>
</body>
</html>
Latihan
Tugas-1 Perorangan
6
Modul Pemrograman PHP & MySQL
Bab 6
PHP If...Else Statements
Statement If-else dalam PHP digunakan untuk melakukan bermacam aksi berdasarkanpada
beragam kondisi pada program.
Statemen If...Else
Sintaks
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Program6-1.php
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
Program6-2.php
<html>
<body>
<?php
$d=date("A");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
Statemen ElseIf
Sintaks
if (condition)
7
Modul Pemrograman PHP & MySQL
Program6-3.php
<html>
<title>Operator Aritmetika</title>
<body>
<?php
$nilai = 95;
$keterangan="Tidak Lulus";
if ($nilai>= 60) {
$keterangan = "Lulus";
}
printf("Nilai = %d<br>",$nilai);
printf("keterangan= %s<br>",$keterangan);
?>
</body>
</html>
Program6-4.php
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
Latihan
8
Modul Pemrograman PHP & MySQL
Bab 7
Switch
Switch digunakan untuk melakukan suatu aksi dari beberapa aksi yang berbeda berdasarkan
pada satu atau lebih kondisi yang berbeda.
Sintaks
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
Program7-1.php
<html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";}
?>
</body>
</html>
Latihan
Buat Program berikut
Bulan ke-1 Januari Bulan ke-6 Juli
Bulan ke-2 Pebruari Bulan ke-7 Agustus
Bulan ke-3 Maret Bulan ke-8 September
Bulan ke-4 April Bulan ke-9 Oktober
Bulan ke-5 Mei Bulan ke-10 Nopember
Bulan ke-5 Juni Bulen ke-12 Desember
9
Modul Pemrograman PHP & MySQL
Bab 9
Looping/Pengulangan
Statemen Looping statements digunakan untuk mengeksekusi blok program yang sama
beberapa kali.
Jenis-jenis Looping
while
do...while
for
foreach
Statemen while
Sintaks
while (condition)
{
code to be executed;
}
Program9-1.php
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
Statemen do...while
do
{
code to be executed;
}
while (condition);
Program9-2.php
<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
10
Modul Pemrograman PHP & MySQL
?>
</body>
</html>
Statemen for
Statatemen for bekerja sebagai berikut :
1. Inisialisasi sebagai nilai awal
2. Kondisi diuji, jika bernilai true (benar),maka perulangan dilakukan dengan
mengerjakan blok pernyataan jika bernilai false (salah) maka perulangan berhenti dan
blok pernyataan dilompati
3. Jika blok pernyataan hanya terdiri satu baris maka tanda kurung kurawal dapat
ditiadakan
4. Increment merupakan nilai penambahan atau pengurangan untuk mengulangi
pengerjaan blok pernyataan jika kondisi masih terpenuhhi.
For (initialization; condition; increment)
{
code to be executed;
}
Program9-3.php
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>
Statemen foreach
Program9-4.php
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
11
Modul Pemrograman PHP & MySQL
Latihan
1. Buat Program menggunakan statemen while dan statemen for dengan output
sebagai berikut
1 3 5 7 9 11 13 15 17
2 4 8 16 32 64
Tugas-2 Perorangan
1. Buat program menggunakan statemen while dan statemen for dengan output sebagai
berikut :
a. -2 4 -8 16 -32 64 -128
b. 1 1 2 3 5 8 13 21 34 {bilangan fibonanci}
12
Modul Pemrograman PHP & MySQL
Bab 10
Fungsi/Function
Fungsi merupakan sebuah blok program yang dapat dieksekusi kapanpun kita memerlukannya.
Program10-1.php
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
writeMyName();
?>
</body>
</html>
Program10-2.php:
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>
Ouput program:
Hello world!
My name is Kai Jim Refsnes.
That's right, Kai Jim Refsnes is my name.
Fungsi berparameter
Program10-3.php
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Refsnes.<br />";
}
echo "My name is ";
13
Modul Pemrograman PHP & MySQL
writeMyName("Kai Jim");
echo "My name is ";
writeMyName("Hege");
echo "My name is ";
writeMyName("Stale");
?>
</body>
</html>
output program:
My name is Kai Jim Refsnes.
My name is Hege Refsnes.
My name is Stale Refsnes.
Program10-4.php
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Refsnes" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("Kai Jim",".");
echo "My name is ";
writeMyName("Hege","!");
echo "My name is ";
writeMyName("Stle","...");
?>
</body>
</html>
output:
My name is Kai Jim Refsnes.
My name is Hege Refsnes!
My name is Stle Refsnes...
14
Modul Pemrograman PHP & MySQL
output :
1 + 16 = 17
15
Modul Pemrograman PHP & MySQL
Bab 11
PHP Forms and User Input
Perintah $_GET dan $_POST digunakan untuk mengirim informasi dari form, seperti input user.
Program11-1.php
<html>
<body>
<form action="Program11-2.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
Program11-2.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
output:
Welcome John.
You are 28 years old.
16
Modul Pemrograman PHP & MySQL
Bab 12
PHP $_GET
variabel $_GET digunakan untuk mengambil nilai dari form menggunakan metode get.
Variabel $_GET
Program12-1.php
<form action="Program12-2.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Ketika user mengklik tombol submit, URL yang dikirm akan berbentuk seperti di bawah ini.
http://localhost/Program12-2.php?name=Peter&age=37
Program12-2.php
Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
Note: Dengan menggunakan $_GET, nama variabel dan nilainya akan ditampilkan di address
bar.
Note: $_GET tidak dapat digunakan untuk mengirim variabel yang besar, nilai yang dapat
dikirim tidak dapat melebihi 100 karakter.
Variabel $_REQUEST
Program12-3.php
Welcome <?php echo $_REQUEST["name"]; ?>.<br />
You are <?php echo $_REQUEST["age"]; ?> years old!
17
Modul Pemrograman PHP & MySQL
Bab 13
PHP $_POST
Variabel $_POST digunakan untuk mengambil data dari form yang dikirim menggunakan
metode post.
Program13-1.php
<form action="Program13-2.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
Program13-2.php
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
Variabel $_REQUEST
Program13-3.php
Welcome <?php echo $_REQUEST["name"]; ?>.<br />
You are <?php echo $_REQUEST["age"]; ?> years old!
18
Modul Pemrograman PHP & MySQL
Bab 14
PHP Date()
Sintaks
date(format,timestamp)
Parameter Keterangan
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time (as a
timestamp)
Timestamp
Timestamp adalah jumlah detik sejak January 1, 1970 00:00:00 GMT. Juga dikenal sebagai Unix
Timestamp.
Format Tanggal
Program14-1.php
<?php
echo date("Y/m/d");
echo "<br />";
echo date("Y.m.d");
echo "<br />";
echo date("Y-m-d");
?>
output:
2006/07/11
2006.07.11
2006-07-11
Fungsi mktime() akan memberikan nilai Unix timestamp untuk tanggal tertentu.
Sintaks
mktime(hour,minute,second,month,day,year,is_dst)
Program14-2.php
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d/", $tomorrow);
?>
output:
Tomorrow is 2006/07/12
19
Modul Pemrograman PHP & MySQL
Bab 15
PHP Include File
Server Side Includes (SSI) digunakan untuk menyimpan fungsi, header, footer, atau elemen-
elemen yang dapat digunakan pada halaman yang berlainan.
Fungsi include()
Fungsi include() akan mengambil semua teks pada file include dan mengkopinya ke file tujuan.
Program15-1.php
Diasumsikan bahwa kita mempunyai file header dengan nama header.php. Untuk memakai
file ini pada halaman web kita seperti di bawah ini.
<html>
<body>
<?php include("header.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>
Program15-2.php
Sekarang, kita asumsikan bahwa kita mempunyai file standar menu yang akan digunakan pada
seluruh halaman (file include biasanya berektensi *.php). Penggunaannya seperti di bawah ini.
<html>
<body>
<a href="http://www.w3schools.com/default.php">Home</a> |
<a href="http://www.w3schools.com/about.php">About Us</a> |
<a href="http://www.w3schools.com/contact.php">Contact Us</a>
Ketiga file, "default.php", "about.php", dan "contact.php" semuanya akan di-include-kan pada
file "menu.php". Berikut ini program "default.php":
<?php include("menu.php"); ?>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>
20
Modul Pemrograman PHP & MySQL
Fungsi require()
Fungsi require() sama dengan include(), tetapi berbeda dalam cara penanganan kesalahan.
Fungsi include() akan menghasilkan peringatan (dan program akan melanjutkan ekseskusinya)
sedangkan fungsi require() akan menghasilkan fatal error dan menghentikan program.
Program15-3.php (program contoh error pada penggunaan fungsi include()).
<html>
<body>
<?php
include("wrongFile.php");
echo "Hello World!";
?>
</body>
</html>
Error message:
Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
Hello World!
<?php
require("wrongFile.php");
echo "Hello World!";
?>
</body>
</html>
Error message:
Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5
Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5
21
Modul Pemrograman PHP & MySQL
Bab 16
PHP File Handling
Membuka File
Program16-1.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
Program16-2.php
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
</body>
</html>
Menutup File
Program16-3.php
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
22
Modul Pemrograman PHP & MySQL
Catatan: Kita tidak dapat membaca file yang terbuka dalam mode w, a, dan x!
if (feof($file)) echo "End of file";
Program16-4.php
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
Program16-5.php
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
23
Modul Pemrograman PHP & MySQL
Bab 17
PHP File Upload
Program17-1.php
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload_file.php
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
Dengan menggunakan array global PHP $_FILES kita dapat meng-upload file dari client ke
server.
Parameter pertama adalah nama input dan yang kedua adalah dapat berupa "name", "type",
"size", "tmp_name" atau "error". Seperti berikut ini:
$_FILES["file"]["name"] Nama file yang akan di-upload.
$_FILES["file"]["type"] Type dari file yang akan di-upload.
$_FILES["file"]["size"] Ukuran dalam byte dari file yang akan di-upload.
$_FILES["file"]["tmp_name"] Nama kopian sementara dari file yang disimpan di
server.
$_FILES["file"]["error"] Kode error dari file yang di-upload.
Hal ini sangat mudah untuk dilakukan. Untuk alas an keamanan, kita seharusnya menerapkan
kebijakan siapa saja user yang dapat meng-upload file ke server.
24
Modul Pemrograman PHP & MySQL
Program17-2.php
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}
?>
Program17-3.php
<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
25
Modul Pemrograman PHP & MySQL
26
Modul Pemrograman PHP & MySQL
Bab 18
PHP Cookies
Program18-1.php
<?php
setcookie("user", "Alex Porter", time()+3600);
?>
<html>
<body>
</body>
</html>
Program18-2.php
<?php
// Print a cookie
echo $_COOKIE["user"];
// A way to view all cookies
print_r($_COOKIE);
?>
Program18-3.php menggunakan fungsi isset() untuk mencari apakah cookie telah dibuat.
<html>
<body>
<?php
if (isset($_COOKIE["user"]))
echo "Welcome " . $_COOKIE["user"] . "!<br />";
else
echo "Welcome guest!<br />";
?>
</body>
</html>
Menghapus Cookie
Program18-4.php
<?php
// set the expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
27
Modul Pemrograman PHP & MySQL
welcome.php.
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
28
Modul Pemrograman PHP & MySQL
Bab 19
PHP Sessions
Session digunakan untuk menyimpan informasi mengenai, atau merubah setting dari sesi
seorang user. Variabel session menyimpan informasi mengenai seorang user, dan
menyediakan informasi tersebut pada seluruh halaman web pada suatu aplikasi .
Ketika kita membuat suatu aplikasi, kita membuka aplikasi tersebut kemudian melakukan beberapa
perkerjaan hingga akhirnya menutup aplikasi tersebut. Aplikasi akan mengetahui siapa kita. Aplikasi
mengetahui ketika kita mulai menjalankan aplikasi dan ketika kita menutup aplikasi tersebut. Tetapi pada
aplikasi internet terdapat satu kesulitan, yaitu: web server tidak dapat mengetahui siapa user yang
menggunakan aplikasinya dan apakah yang sedang kita lakukan karena alamat HTTP tidak menyimpan
state aplikasi.
Mekanisme session pada PHP akan menyelesaikan permasalahan ini dengan cara memperbolehkan
aplikasi untuk menyimpan informasi pada server untuk digunakan kemudian (contohnya: username,
shopping items dll). Bagaimanapun, informasi session bersifat sementara dan akan dihapus setelah user
meninggalkan situs yang digunakan. Jika kita membutuhkan informasi yang bersifat permanent, kita dapat
menyimpan data pada database.
Session bekerja dengan cara membuat Unique ID (UID) untuk setiap user dan menyimpan variabel
berdasarkan UID tersebut. UID disimpan baik pada cookie atau URL.
Program19-1.php
<?php session_start(); ?>
<html>
<body>
</body>
</html>
Kode di atas akan mendaftarkan session user ke server, dan mengijinkan kita untuk memulai
menyimpan informasi user dan memebuat UID untuk session user tersebut.
Cara yang tepat untuk menyimpan dan mengambil variabel session adalah menggunakan
variabel $_SESSION:
Program19-2.php
<?php
session_start();
// store session data
$_SESSION['views']=1;
?>
<html>
<body>
<?php
//retrieve session data
echo "Pageviews=". $_SESSION['views'];
?>
29
Modul Pemrograman PHP & MySQL
</body>
</html>
Output:
Pageviews=1
Pada contoh di bawah ini, kita akan membuat counter page-views sederhana. Fungsi isset()
akan melakukan pengecekan terhadap variabel views apakah telah dibuat. Jika views telah
tersedia, maka kita dapat melakukan proses penambahan pada counter. Jika views tidak ada,
maka kita akan membuat variabel views dan mensetnya dengan 1.
Program19-3.php
<?php
session_start();
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>
Menghapus Session
Jika kita ingin menghapus beberapa data session, kita dapat menggunakan fungsi unset() atau
fungsi session_destroy().
Program19-4.php
<?php
unset($_SESSION['views']);
?>
Program19-5.php
<?php
session_destroy();
?>
Catatan: session_destroy() akan me-reset session kita dan kita akan kehilangan seluruh data
session yang telah tersimpan.
30
Modul Pemrograman PHP & MySQL
Bab 20
Pengenalan PHP MySQL
Tabel
Database dapat terdiri dari satu atau lebih tabel. Setiap table mempunyai nama. Setiap tabel
mengandung record dan data. Di bawah ini contoh tabel "Persons":
Query
Dengan MySQL, kita dapat melakukan query untuk mendapatkan informasi tertentu dari
database.
Contoh query
SELECT LastName FROM Persons
Query di atas akan menampilkan semua data pada field LastName dari tabel Persons.
LastName
Hansen
Svendson
Pettersen
Download MySQL
31
Modul Pemrograman PHP & MySQL
Bab 21
Koneksi PHP MySQL
mysql_connect(servername,username,password);
Parameter Description
servername Optional. Specifies the server to connect to. Default value is
"localhost:3306"
username Optional. Specifies the username to log in with. Default value is the name
of the user that owns the server process
password Optional. Specifies the password to log in with. Default is ""
Program23-1.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
?>
Menutup Koneksi
Program23-2.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
32
Modul Pemrograman PHP & MySQL
Bab 22
Membuat Database dan Tabel
Membuat Database
Program24-1.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
Membuat Tabel
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
.......
)
Program24-2.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table in my_db database
33
Modul Pemrograman PHP & MySQL
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Person
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
mysql_close($con);
?>
34
Modul Pemrograman PHP & MySQL
35
Modul Pemrograman PHP & MySQL
Bab 23
PHP MySQL Insert Into
INSERT INTO pernyataan yang digunakan untuk memasukkan record baru ke dalam tabel
database
Insert Data Into a Database Table
atau
Program25-1.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>
Program25-2.php
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
insert.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
36
Modul Pemrograman PHP & MySQL
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
37
Modul Pemrograman PHP & MySQL
BAB 24
PHP MySQL Select
Syntax
SELECT column_name(s)
FROM table_name
Catatan: SQL pernyataan tidak sensitif huruf. SELECT sama pilih.
Untuk mendapatkan PHP untuk menjalankan pernyataan di atas kita harus menggunakan
mysql_query () function. Fungsi ini digunakan untuk mengirim query atau perintah ke koneksi
MySQL.
Contoh:
Contoh berikut memilih semua data yang tersimpan dalam "Person" table (The karakter *
memilih semua data dalam tabel):
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>
Contoh di atas menyimpan data yang dikembalikan oleh mysql_query () fungsi dalam variabel
$ hasil. Selanjutnya, kita menggunakan mysql_fetch_array () fungsi kembali ke baris pertama
dari recordset sebagai array. Setiap panggilan ke mysql_fetch_array () kembali dengan baris
berikutnya di recordset. Sementara yang loops lingkaran melalui semua catatan dalam
recordset. Untuk mencetak nilai dari setiap baris, kita gunakan variabel PHP $ row ($ row [
'firstname'] dan $ baris [ 'belakang']).
Output dari kode di atas akan:
Peter Griffin
Glenn Quagmire
38
Modul Pemrograman PHP & MySQL
mysql_select_db("my_db", $con);
39
Modul Pemrograman PHP & MySQL
BAB 25
PHP MySQL The Where Clause
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example
The following example will select all rows from the "Person" table, where FirstName='Peter':
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
?>
The output of the code above will be:
Peter Griffin
40
Modul Pemrograman PHP & MySQL
41
Modul Pemrograman PHP & MySQL
BAB 26
PHP MySQL Order By Keyword
mysql_select_db("my_db", $con);
while($row = mysql_fetch_array($result))
{
echo $row['FirstName']
echo " " . $row['LastName'];
echo " " . $row['Age'];
echo "<br />";
}
mysql_close($con);
?>
The output of the code above will be:
Glenn Quagmire 33
Peter Griffin 35
Use the DESC keyword to specify a descending sort-order (9 before 1 and "p" before "a"):
SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC
42
Modul Pemrograman PHP & MySQL
43
Modul Pemrograman PHP & MySQL
BAB 27
PHP MySQL Update
Note: SQL statements are not case sensitive. UPDATE is the same as update.
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example
Earlier in the tutorial we created a table named "Person". Here is how it looks:
FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33
The following example updates some data in the "Person" table:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
44
Modul Pemrograman PHP & MySQL
BAB 28
PHP MySQL Delete From
The DELETE FROM statement is used to delete records from a database table.
Syntax
DELETE FROM table_name
WHERE column_name = some_value
Note: SQL statements are not case sensitive. DELETE FROM is the same as delete from.
To get PHP to execute the statement above we must use the mysql_query() function. This
function is used to send a query or command to a MySQL connection.
Example
Earlier in the tutorial we created a table named "Person". Here is how it looks:
FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33
The following example deletes all the records in the "Person" table where LastName='Griffin':
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
45