Prak - 9 Jobsheet PHP Mysql
Prak - 9 Jobsheet PHP Mysql
JOBSHEET PW
KONEKTIVITAS PHP DENGAN MySQL
Tujuan :
1. Memahami langkah-langkah koneksi PHP dengan MySQL.
2. Memahami perbedaan pengambilan record dari database.
Sintaks :
$conn adalah nama variabel penampung status hasil koneksi kepada database.
host adalah nama host atau alamat server database MySQL.
username adalah nama user yang telah diberi hak untuk dapat mengakses server
database.
password adalah kata sandi untuk username untuk dapat masuk ke dalam
database.
Sintaks :
$pilih = mysql_select_db(”namadatabase”,$conn);
1
Praktikum PW | POLINDRA | Zhaly LA
Sintaks :
$hasil akan berupa record set apabila SQL Statement berupa perintah select.
Sintaks :
$row = mysql_fetch_array($hasil);
$row adalah array satu record dari record $hasil yang diproses nomor record
sesuai dengan nomor urut dari proses mysql_fetch_array yang sedang
dilakukan.
$hasil adalah record set yang akan diproses.
b. mysql_fetch_assoc()
Fungsi ini hampir sama dengan fungsi mysql_fetch_array(), hanya saja array yang
dihasilkan hanya array asosiatif.
Sintaks :
$row = mysql_fetch_assoc($hasil);
c. mysql_fetch_row()
Fungsi ini hampir sama dengan fungsi mysql_fetch_array(), hanya saja array yang
dihasilkan hanya array numeris.
Sintaks :
$row = mysql_fetch_row($hasil);
2
Praktikum PW | POLINDRA | Zhaly LA
d. mysql_num_rows()
Fungsi ini digunakan untuk menghitung jumlah record yang ada pada database.
Sintaks :
$jml = mysql_num_rows($hasil);
$jml akan memiliki nilai sesuai dengan jumlah record yang ada.
LATIHAN – LATIHAN :
a. Buatlah tabel liga berikut ini, dengan 3 field : kode, negara, champion.
Create table liga (
kode char(3) not null,
negara char(15),
champion int
);
3
Praktikum PW | POLINDRA | Zhaly LA
<HTML><HEAD>
<title>Koneksi Database MySQL</title>
</HEAD>
<BODY>
<h1>Koneksi database dengan mysql_fetch_array</h1>
<?
$conn=mysql_connect ("localhost","root","")
or die ("koneksi gagal");
mysql_select_db("faruq",$conn);
$hasil = mysql_query("select * from liga",$conn);
while ($row=mysql_fetch_array($hasil)) {
echo "Liga " .$row["negara"]; //array asosiatif
echo " mempunyai " .$row[2]; //array numeris
echo " wakil di liga champion <br>";
}
?>
</BODY></HTML>
<HTML><HEAD>
<title>Koneksi Database MySQL</title>
</HEAD>
<BODY>
<h1>Koneksi database dengan mysql_fetch_assoc</h1>
<?
$conn=mysql_connect ("localhost","root","")
or die ("koneksi gagal");
mysql_select_db("faruq",$conn);
$hasil = mysql_query("select * from liga",$conn);
while ($row=mysql_fetch_array($hasil)) {
echo "Liga " .$row["negara"];
echo " mempunyai " .$row[“champion”];
echo " wakil di liga champion <br>";
}
?>
</BODY></HTML>
4
Praktikum PW | POLINDRA | Zhaly LA
<HTML>
<HEAD>
<title>Koneksi Database MySQL</title>
</HEAD>
<BODY>
<h1>Koneksi database dengan mysql_fetch_assoc</h1>
<?
$conn=mysql_connect ("localhost","root","")
or die ("koneksi gagal");
mysql_select_db("faruq",$conn);
$hasil = mysql_query("select * from liga",$conn);
while ($row=mysql_fetch_row($hasil)) {
echo "Liga " .$row[1];
echo " mempunyai " .$row[2];
echo " wakil di liga champion <br>";
}
?>
</BODY>
</HTML>
6
Praktikum PW | POLINDRA | Zhaly LA
<HTML>
<HEAD>
<title>Cari Database</title>
</HEAD>
<BODY>
<h1>Searching Buku Tamu untuk database MySQL</h1>
<form action="hasilsearch.php" method="post">
<select name="kolom">
<option value="nama">nama</option>
<option value="email">email</option>
</select>
Masukkan kata yang anda cari
<input type ="text" type ="text" name="cari">
<input type ="submit" value="cari" >
</form>
</BODY>
</HTML>
7
Praktikum PW | POLINDRA | Zhaly LA
<?php
// we must never forget to start the session
session_start();
$errorMessage = '';
if (isset($_POST['txtUserId']) &&
isset($_POST['txtPassword'])) {
// check if the username and password combination is
correct
if ($_POST['txtUserId'] === 'admin' &&
$_POST['txtPassword'] === 'udinharun') {
// the username and password match,
// set the session
$_SESSION['basic_is_logged_in'] = true;
8
Praktikum PW | POLINDRA | Zhaly LA
}
-->
</style>
</head>
<body>
<?php
if ($errorMessage !=''){
?>
<p><div align="center"><span class="style5"><?php echo
$errorMessage; ?>
</span> </p>
<?php
}
?>
</div>
</div>
<form action="" method="post" name="frmLogin"
id="frmLogin">
<table width="400" border="1" align="center">
<tr>
<th width="160" scope="col"><div align="left">User Id
</div></th>
<th width="224" scope="col"><div align="left">
<input name="txtUserId" type="text" id="txtUserId">
</div></th>
</tr>
<tr>
<td><div align="left">Password</div></td>
<td><div align="left">
<input name="txtPassword" type="password"
id="txtPassword">
</div></td>
</tr>
<tr>
<td> </td>
<td><input name="btnLogin" type="submit"
id="btnLogin" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
9
Praktikum PW | POLINDRA | Zhaly LA
main1.php
<?php
// like i said, we must never forget to start the session
session_start();
?>
<html>
<head>
<title>Main User Page</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<p>This is the main application page. You are free to
play around here since you
are an autenthicated user :-) </p>
<p> </p>
<p><a href="logout1.php">Logout</a> </p>
</body>
</html>
logout1.php
<?php
// i will keep yelling this
// DON'T FORGET TO START THE SESSION !!!
session_start();
10
Praktikum PW | POLINDRA | Zhaly LA
login2.php
<?php
// we must never forget to start the session
session_start();
$errorMessage = '';
if (isset($_POST['txtUserId']) &&
isset($_POST['txtPassword'])) {
include 'library/config.php';
include 'library/opendb.php';
$userId = $_POST['txtUserId'];
$password = $_POST['txtPassword'];
if (mysql_num_rows($result) == 1) {
// the user id and password match,
// set the session
$_SESSION['db_is_logged_in'] = true;
11
Praktikum PW | POLINDRA | Zhaly LA
include 'library/closedb.php';
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<?php
if ($errorMessage != '') {
?>
<p align="center"><strong><font color="#990000"><?php
echo $errorMessage; ?></font></strong></p>
<?php
}
?>
<form action="" method="post" name="frmLogin"
id="frmLogin">
<table width="400" border="1" align="center"
cellpadding="2" cellspacing="2">
<tr>
<td width="150">User Id</td>
<td><input name="txtUserId" type="text"
id="txtUserId"></td>
</tr>
<tr>
<td width="150">Password</td>
<td><input name="txtPassword" type="password"
id="txtPassword"></td>
</tr>
<tr>
<td width="150"> </td>
<td><input name="btnLogin" type="submit" id="btnLogin"
value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
main2.php
12
Praktikum PW | POLINDRA | Zhaly LA
<?php
// like i said, we must never forget to start the session
session_start();
?>
<html>
<head>
<title>Main User Page</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
</head>
<body>
<p>Main 2 Sukses - login dengan database <br>
This is the main application page. You are free to play
around here since you
are an autenthicated user :-) </p>
<p> </p>
<p><a href="logout2.php">Logout</a> </p>
</body>
</html>
config.php
<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'database';
13
Praktikum PW | POLINDRA | Zhaly LA
?>
opendb.php
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
('Error connecting to mysql');
mysql_select_db($dbname);
?>
closedb.php
<?php
mysql_close($conn);
?>
14
Praktikum PW | POLINDRA | Zhaly LA
TUGAS :
Modifikasilah program diatas, sehingga anda memiliki 3 tombol pada website pribadi,
yaitu:
1. Bukutamu untuk inputkan data
2. Tampilkan untuk menampilkan database bukutamu dalam bentuk tabel.
3. Search untuk melakukan searching database bukutamu dalam bentuk tabel.
15