Professional Documents
Culture Documents
Mirzam
Mirzam
Laporan PPL
1. File MySQLConnection.php $query = "INSERT INTO data (username, password, gender, job)
<?php VALUES ( '$username', '$password', '$gender', '$job' )";
/* Class : MySQLConnection } else {
* Filename : MySQLConnection.php $query = "";
* Kelas koneksi ke MySQL dengan PHP Object }
* @author : mirzam muhammad $con->buatQuery($query,"ppl");
* @NIM: M0508051 ?>
*/ </body>
class MySQLConnection { </html>
//properties
private $connection;
private $database;
private static $instances = 0;
//methods
public function __construct($hostname,$username,$password){
if(MySQLConnection::$instances==0){
$this->connection = mysql_connect($hostname,$username,$password) or
die (mysql_error()."No Kesalahan".mysql_errno());
MySQLConnection::$instances=1;
} else{
$msg="Tutup instansiasi yang lain dari".
"kelas MySQLConnection.";
die($msg);
}} 2. File form.inc
//destructor <?php
public function __destruct(){ /**
$this->close(); * Class : Form
} * Filename : form.inc
//header fungsi untuk membuat query * Sebuah kelas untuk membuat HTML
public function buatQuery($sql,$databasename){ * @author mirzam muhammad
$this->database=$databasename; * @NIM: M0508051
$dat=mysql_select_db($this->database,$this->connection); */
if(!$dat) class Form{
{ //properties
echo "Tidak Ada Database <br />"; private $fields=array(); # nama field dan label
} else { private $processor; #nama file pemroses
echo "Database OK <br />"; private $submit="Submit Form";
} private $NFields=0; #banyaknya field yang ditambahkan
if (mysql_query($sql,$this->connection)){ //methods
$this->mysql_res = mysql_query($sql); public function __construct($filepemroses,$lblsubmit){
echo "Table berhasil dibuat <br />"; $this->processor=$filepemroses;
} else { $this->submit=$lblsubmit;
echo "Table Gagal dibuat <br />"; }
}} public function addField($nama,$lbl){
//fungsi untuk menutup koneksi $this->fields[$this->NFields]['name']=$nama;
public function close(){ $this->fields[$this->NFields]['label']=$lbl;
MySQLConnection::$instances=0; $this->NFields = $this->NFields+1;
if(isset($this->connection)){ }
mysql_close($this->connection); public function addFieldType($nama,$lbl,$type){
unset($this->connection); $this->fields[$this->NFields]['name']=$nama;
}}}?> $this->fields[$this->NFields]['label']=$lbl;
$this->fields[$this->NFields]['type']=$type;
File test.php $this->NFields = $this->NFields+1;
<html> }
<head> public function addFieldValue($nama,$lbl,$tipe,$val) {
<meta http-equiv="content-type" content="text/html; charset=iso-8859- $this->fields[$this->NFields]['name']=$nama;
1" /> $this->fields[$this->NFields]['label']=$lbl;
<meta name="author" content="Mirzam" /> $this->fields[$this->NFields]['type']=$tipe;
<title>Test Koneksi</title> $this->fields[$this->NFields]['value']=$val;
</head> $this->NFields = $this->NFields+1;
<body> }
<?php public function addFieldValue1($nama,$lbl,$tipe,$val,$valName) {
// Filename : test.php $this->fields[$this->NFields]['name']=$nama;
require 'MySQLConnection.php'; $this->fields[$this->NFields]['label']=$lbl;
$con = new MySQLConnection("localhost", "root", ""); $this->fields[$this->NFields]['type']=$tipe;
if($con){ $this->fields[$this->NFields]['value']=$val;
echo "Koneksi berhasil.<br />"; $this->fields[$this->NFields]['valname']=$valName;
} $this->NFields = $this->NFields+1;
if ( (isset($_POST['username'])) and (isset($_POST['password'])) }
and (isset($_POST['gender'])) and (isset($_POST['job'])) ) { public function displayForm(){
$username = $_POST['username']; echo "<form action='$this->processor' method='post'>";
$password = $_POST['password']; echo "<table>";
$gender = $_POST['gender']; for($j=1;$j<=sizeof($this->fields);$j++){
$job = $_POST['job']; echo "<tr><td align='right'> {$this->fields[$j-1]['label']}: </td>\n";
Mirzam Muhammad (M0508051) 2
Laporan PPL
3. File testShowTbl.php
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-
1" />
<meta name="author" content="Mirzam" />
<title>Test Menampilkan Tabel</title>
</head>
<body>
<?php
error_reporting(0);
// Filename : testShowTbl.php
require 'MySQLConnection.php';
$con = new MySQLConnection("localhost", "root", "");
if($con){
echo "Koneksi berhasil.<br />";
}
$query = "SELECT username, password, gender, job FROM data";
$con->buatQuery("$query","ppl");
$result = $con->mysql_res;
while ($data = mysql_fetch_array( $result )) {
$dump .= '<tr><td>'.$data['username'].'</td>';
$dump .= '<td>'.$data['password'].'</td>';
if ($data['gender'] == 0) {
$dump .= '<td>Female</td>';
} else {
$dump .= '<td>Male</td>';
}
$dump .= '<td>'.$data['job'].'</td></tr>';
}