Cara Singkat Menampilkan Data Record MySQL Memakai PDO Extension dan di komparasi dengan Mysql Extension (Original)di PHP

Kita akan membandingkan antara PDO Extension dan MySQL Extension dalam koding PHP

untuk kerangka table nya bisa anda sesuaikan dengan kebutuhan individu masing masing

Di bawah ini adalah cara biasa yg kita pakai dengan Mysql Extension atau cara standar php ke Mysql

// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("rusun") or die(mysql_error());

// Get all the data from the "example" table
$result = mysql_query("SELECT  no_ktp,nama_penghuni,tgl_daftar FROM penghuni ") or die(mysql_error());  
$no=0;
?>
<table border='1' bgcolor='#CCCCCC'>
     <th>No</th>
  <th>No KTP</th>
        <th>Nama</th>
        <th>Tanggal Daftar</th>
       
<?
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
	
		$no++;
	// Print out the contents of each row into a table
	?>
	 <tr> <td >
                <?php echo $no ?>
            </td>
        <td ><?=$row['no_ktp']?></td>
        <td ><?=$row['nama_penghuni']?></td>
        <td ><?=$row['tgl_daftar']?></td>
        </tr>
      <?  
} 
?>
</table>

dan di bawah ini adalah cara Memakai PDO di PHP dengan Database Mysql

<?php

// Make a MySQL Connection
$dsn = 'mysql: host=localhost; dbname=rusun';
$user = 'root';
$password = '';
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
// Get all the data from the "example" table
$sql= "SELECT  no_ktp,nama_penghuni,tgl_daftar,FROM penghuni ";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
$no=0;
?>
<table border='1'>
     <th>No</th>
  <th>No KTP</th>
        <th>Nama</th>
        <th>Tanggal Daftar</th>      
<?
// keeps getting the next row until there are no more to get
foreach($result as $row){
	
		$no++;
		// Print out the contents of each row into a table
	?>
    <tr> <td valign="top">
                <?php echo $no ?>
            </td>
        <td ><? echo "{$row['no_ktp']}"?></td>
        <td ><? echo "{$row['nama_penghuni']}"?></td>
        <td ><? echo"{$row['tgl_daftar']}"?></td>
           
        </tr><?
		

}
?>
</table>

Pertanyaannya adalah Apa menariknya dari PDO ? 😀

Advertisements

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