Advertisement
stevennathaniel

Tampil Data MYSQL PDO

Nov 21st, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3.     try {
  4.        
  5.         $db=new PDO('mysql:host=localhost;dbname=latihan', "root", "kucing");
  6.        
  7.         $sql = $db->prepare("SELECT * FROM barang");
  8.        
  9.         if($sql->execute()){
  10.            
  11.             $sql->setFetchMode(PDO::FETCH_ASSOC);
  12.         }
  13.        
  14.         // $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.        
  16.        
  17.     } catch (Exception $error) {
  18.        
  19.         echo '<p>', $error->getMessage(), '</p>';
  20.     }
  21.  
  22. ?>
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <meta charset="utf-8">
  27. <meta name="viewpoint" content="width=device-width, initial-scale=1">
  28. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  29. <script src="bootstrap/js/jquery-2.4.1.min.js"></script>
  30. <script src="bootstrap/js/bootstrap.min.js"></script>
  31.  
  32. <style>
  33.  
  34. h1{text-align:center;}
  35.  
  36. table{style="float:right" border"1"}
  37.  
  38.  
  39. </style>
  40.  
  41. <title>Tampilkan Data</title>
  42.  
  43. </head>
  44.  
  45. <body>
  46.  
  47. <div class="container">
  48.  
  49. <h1>Menampilkan Isi Tabel MySQL</h1>
  50.  
  51.  
  52. <table class="table">
  53.  
  54.  
  55.     <thead>
  56.    
  57.         <tr>
  58.        
  59.             <th>ID Barang</th>
  60.             <th>Nama Barang</th>
  61.             <th>Harga Satuan</th>
  62.            
  63.         </tr>
  64.        
  65.     </thead>
  66.    
  67.    
  68.  
  69.     <tbody>
  70.     <?php while($row = $sql->fetch()) { ?>
  71.         <tr>
  72.        
  73.             <td><?php echo $row["idbarang"]; ?></td>
  74.            
  75.             <td><?php echo $row["namabarang"]; ?></td>
  76.            
  77.             <td><?php echo $row["hargasatuan"]; ?></td>
  78.        
  79.         </tr>
  80.    
  81.    
  82. <?php } ?> 
  83.    
  84.     </tbody>
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. </table>
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. </div>
  100.  
  101. </body>
  102.  
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement