OOP example in PDO

Now I want to share how to create example connect to database (example MySQL) using PDO (PHP Data Object). Example I have MySQL structure such as:

mysql> desc nasabah;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| id         | varchar(6)   | NO   | PRI | NULL    |       |
| nama       | varchar(100) | NO   |     | NULL    |       |
| ktp        | varchar(40)  | NO   |     | NULL    |       |
| tmpt_lahir | varchar(100) | YES  |     | NULL    |       |
| tgl_lahir  | date         | YES  |     | NULL    |       |
| alamat     | text         | YES  |     | NULL    |       |
| telepon    | varchar(15)  | YES  |     | NULL    |       |
| email      | varchar(200) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)

mysql> 


Next, we must create a file for connected to database MySQL (file test.php):

<?php
class Database {
	protected $host="localhost";
 	protected $user="imron";
 	protected $db="angsuranbang";
 	protected $pass="30052009";
 	protected $conn;

 	public function __construct() {
 		$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
 	}
}

class Nasabah extends Database {
	//method result nasabah
	public function show_nasabah() {
		$sql = "SELECT * FROM nasabah ORDER BY id";
		$q = $this->conn->prepare($sql);
		$q->execute();
		return $row = $q->fetchAll();
	}
}
?>

And the last, we must create for result pdo query from test.php on class Nasabah (file test1.php):

<?php

//load file
include_once('test.php');

$db = new Nasabah();
?>
<html>
<head>
	<title>Test</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>No</th>
			<th>Id Nasabah</th>
			<th>Nama Nasabah</th>
			<th>Tempat Tanggal Lahir</th>
			<th>Alamat</th>
			<th>Telepon</th>
		</tr>
		<?php
		$arrayNasabah = $db->show_nasabah();
		
		if (count($arrayNasabah)) {
			foreach ($arrayNasabah as $data) {
				?>
				<tr>
					<td><?php echo $c=$c+1;?>.</td>
					<td><?php echo $data['id'] ?></td>
					<td><?php echo $data['nama'] ?></td>
					<td><?php echo $data['tmpt_lahir'].', '. date("d-m-Y", strtotime($data['tgl_lahir']));?></td>
					<td><?php echo $data['alamat'];?></td>
					<td><?php echo $data['telepon'];?></td>
				</tr>
				<?php
			}
		}
		?>
	</table>
</body>
</html>

Result:
result pdo
For more info about PDO, look at this http://php.net/manual/en/book.pdo.php

4 comments

  1. I was curious if you ever considered changing
    the page layout of your website? Its very well written;
    I love what youve got to say. But maybe you could
    a little more in the way of content so people could connect with it
    better. Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s