mirzam

2
Mirzam Muhammad (M0508051)  Laporan PPL 1 1.  File MySQLConnection.php <?php /* Class : MySQLConnection * Filename : MySQLConnection.php * Kelas koneksi ke MySQL de ngan PHP Objec t * @author : mirzam muhammad * @NIM: M0508051 */ class MySQLConnection { //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); }} //destructor public function __destruct(){ $this->close(); } //header fungsi untuk membuat query public function buatQuery($sql,$databasename){ $this->database=$databasename; $dat=mysql_select_db($this->database,$this->connection); if(!$dat) { echo "Tidak Ada Database <br />"; } else { echo "Database OK <br />"; } if (mysql_query($sql,$this ->connection)){ $this->mysql_res = mysql_query($sql); echo "Table berhasil dibuat <br />"; } else { echo "Table Gagal dibuat <br />"; }} //fungsi untuk menutup koneksi public function close(){ MySQLConnection::$instances=0; if(isset($this->connection)){ mysql_close($this->connection); unset($this->connection); }}}?> File test.php <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859- 1" /> <meta name="author" content="Mirzam" /> <title>Tes t Koneksi</title> </head> <body> <?php // Filename : test.php require 'MySQLConnection.php'; $con = new MySQLConnection("localhost", "root", ""); if($con){ echo "Koneksi berhasil.<br />"; } if ( (isset($_POST['username'])) and (isset($_POST['password'])) and (isset($_POST['gender'])) and (isset($_POST['job'])) ) { $username = $_POST['username']; $password = $_POST['password']; $gender = $_POST['gender']; $job = $_POST['job']; $query = "INSERT INTO data (username, password, gender, job) VALUES ( '$username', '$password', '$gender', '$job' )";  } else { $query = ""; } $con->buatQuery($query,"ppl"); ?> </body> </html> 2.  File form.inc  <?php /** * Class : Form * Filename : form.inc * Sebuah kelas untuk membuat HTML * @author mirzam muhammad * @NIM: M0508051 */ class Form{ //properties private $fields=array(); # nama field dan label private $processor; #nama file pemroses private $submit="Submit Form"; private $NFields=0; #banyaknya field yang ditambahkan //methods public function __construct($filepemroses,$lblsubmit){ $this->processor=$filepemroses; $this->submit=$lblsubmit; } public function addField($nama,$lbl){ $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->NFields = $ this ->NFields+1; } public function addFieldType($nama,$lbl,$type){ $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$type; $this->NFields = $ this->NFields+1; } public function addFieldValue($nama,$lbl,$tipe,$val) { $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$tipe; $this->fields[$this->NFields]['value']=$val; $this->NFields = $ this->NFields+1; } public function addFieldValue1($nama,$lbl,$tipe,$val,$valName) { $this->fields[$this->NFields]['name']=$nama; $this->fields[$this->NFields]['label']=$lbl; $this->fields[$this->NFields]['type']=$tipe; $this->fields[$this->NFields]['value']=$val; $this->fields[$this->NFields]['valname']=$valName; $this->NFields = $ this->NFields+1; } public function displayForm(){ echo "<form action='$this->processor' method='post'>"; echo "<table>"; for($j=1;$j<=sizeof($this->fields);$j++){ echo "<tr><td align='right'> {$ this ->fields[$j-1]['label']}: </td>\n" ;

Upload: elvin-kusuma

Post on 08-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

8/7/2019 mirzam

http://slidepdf.com/reader/full/mirzam 1/2

Mirzam Muhammad (M0508051) 

Laporan PPL

1.  File MySQLConnection.php

<?php

/* Class : MySQLConnection

* Filename : MySQLConnection.php

* Kelas koneksi ke MySQL dengan PHP Object

* @author : mirzam muhammad

* @NIM: M0508051

*/ 

class MySQLConnection {//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);

}}

//destructor

public function __destruct(){$this->close();

}

//header fungsi untuk membuat query

public function buatQuery($sql,$databasename){

$this->database=$databasename;

$dat=mysql_select_db($this->database,$this->connection);

if(!$dat)

{

echo "Tidak Ada Database <br />";

} else {

echo "Database OK <br />";

}

if (mysql_query($sql,$this->connection)){

$this->mysql_res = mysql_query($sql);

echo "Table berhasil dibuat <br />";

} else {

echo "Table Gagal dibuat <br />";

}}

//fungsi untuk menutup koneksi

public function close(){

MySQLConnection::$instances=0;

if(isset($this->connection)){

mysql_close($this->connection);

unset($this->connection);

}}}?>

File test.php

<html>

<head>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-

1" />

<meta name="author" content="Mirzam" />

<title>Test Koneksi</title></head>

<body>

<?php

// Filename : test.php

require 'MySQLConnection.php';

$con = new MySQLConnection("localhost", "root", "");

if($con){

echo "Koneksi berhasil.<br />";

}

if ( (isset($_POST['username'])) and (isset($_POST['password']))

and (isset($_POST['gender'])) and (isset($_POST['job'])) ) {

$username = $_POST['username'];

$password = $_POST['password'];

$gender = $_POST['gender'];

$job = $_POST['job'];

$query = "INSERT INTO data (username, password, gender, job)

VALUES ( '$username', '$password', '$gender', '$job' )"; 

} else {

$query = "";

}

$con->buatQuery($query,"ppl");

?>

</body>

</html>

2.  File form.inc  

<?php

/**

* Class : Form

* Filename : form.inc

* Sebuah kelas untuk membuat HTML

* @author mirzam muhammad

* @NIM: M0508051

*/

class Form{

//properties

private $fields=array(); # nama field dan label

private $processor; #nama file pemroses

private $submit="Submit Form";

private $NFields=0; #banyaknya field yang ditambahkan

//methods

public function __construct($filepemroses,$lblsubmit){

$this->processor=$filepemroses;

$this->submit=$lblsubmit;}

public function addField($nama,$lbl){

$this->fields[$this->NFields]['name']=$nama;

$this->fields[$this->NFields]['label']=$lbl;

$this->NFields = $this->NFields+1;

}

public function addFieldType($nama,$lbl,$type){

$this->fields[$this->NFields]['name']=$nama;

$this->fields[$this->NFields]['label']=$lbl;

$this->fields[$this->NFields]['type']=$type;

$this->NFields = $this->NFields+1;

}

public function addFieldValue($nama,$lbl,$tipe,$val) {

$this->fields[$this->NFields]['name']=$nama;

$this->fields[$this->NFields]['label']=$lbl;

$this->fields[$this->NFields]['type']=$tipe;

$this->fields[$this->NFields]['value']=$val;$this->NFields = $this->NFields+1;

}

public function addFieldValue1($nama,$lbl,$tipe,$val,$valName) {

$this->fields[$this->NFields]['name']=$nama;

$this->fields[$this->NFields]['label']=$lbl;

$this->fields[$this->NFields]['type']=$tipe;

$this->fields[$this->NFields]['value']=$val;

$this->fields[$this->NFields]['valname']=$valName;

$this->NFields = $this->NFields+1;

}

public function displayForm(){

echo "<form action='$this->processor' method='post'>";

echo "<table>";

for($j=1;$j<=sizeof($this->fields);$j++){

echo "<tr><td align='right'> {$this->fields[$j-1]['label']}: </td>\n";

8/7/2019 mirzam

http://slidepdf.com/reader/full/mirzam 2/2