Kaskus

Tech

Pengaturan

Mode Malambeta
Gambar

Lainnya

Tentang KASKUS

Pusat Bantuan

Hubungi Kami

KASKUS Plus

© 2024 KASKUS, PT Darta Media Indonesia. All rights reserved

badycoolAvatar border
TS
badycool
Class PHP Simple dengan PDO (PHP Data Object)
yg blm tahu atau mu belajar PDO , ane kasih contoh sangat simple implementasi PDO ...*saran : mulailah gunakan PDO untuk DB manipulation dgn PHP emoticon-Big Grin, jgn yg native lagi


.Buat DB nya
Pertama kita buat Class untuk Koneksi :

PHP Code:

Class Db_connect{
 
       
//silahkan sesuaikan config dgn dbms masing"
 
protected $dns   "mysql:host=localhost;dbname=test";
 protected 
$db_user  "root";
 protected 
$db_pass  "";
 protected 
$konek  "";
 
 public function 
__construct(){}
 
 public function 
getConnect(){
  
  try{
   
   
$db = new PDO($this->dns,$this->db_user,$this->db_pass);
   if(
$db===FALSE){
   
    throw new 
Exception("Koneksi Gagal");
    
   }else{
   
    
$this->konek $db;
   }
   
  }catch(
Exception $e){
  
   echo 
"Error : ".$e->getMessage();
  }
  return 
$this->konek;
 }
 public function 
closeConnect(){
  
  
$this->konek NULL//diskonek Koneksi
 
}
 
 


Kita buat Class active record, disini ane buat untuk yg fetch Object doang, silahkan tambahkan sendiri

PHP Code:
Class Active_record extends Db_connect{

 public function 
__construct(){}
 
 
 Public function 
fetchObject($sql){
  
  try{

   
$data  =  $this->getConnect()->prepare($sql);
  
   
$data->setFetchMode(PDO::FETCH_INTO,new mhs());
   
$data->execute(); 
   
$clone = array();
   while(
$result $data->fetch()){
    
    
$clone[] = clone $result;
   }
   
$this->closeConnect();
   
  }catch(
PDOException $e){
   echo 
$e->getMessage();
  }
  return 
$clone;
 }




, Skrg kita buat handler nya (modul, apapun itu terserah ). ane contohin Class buat handle modul Mahasiswa

PHP Code:
Class Mhs{
 
 public 
$nama;
 Public 
$nim;
 Public 
$alamat;
 Public 
$id;
 
 public function 
__construct(){
  
  
$this->nama   $this->nama;
  
$this->alamat $this->alamat;
  
$this->nim    $this->nim;
  
$this->id     $this->id;
   
 }
 
 Public function 
getMhs(){
     
  
$objAr  = new Active_record();
  
$sql "select * from mhs";
  
$result $objAr->fetchObject($sql);

  return 
$result;
 } 




baru kita buat untuk tampilan datanya

PHP Code:

 $objMsh 
= new mhs();
 
$table ="<table border="1" style="border-collapse:collapse;">";
 
$table .="<tr>
   
   <th>Id</th>
   <th>Nim</th>
   <th>Nama</th>
   <th>Alamat</th>
  </tr>"
;
  
$data $objMsh->getMhs();
  
reset($data);
  while(list(,
$row) =  each($data)){
   
   
$table .="<tr>
      
      <td>"
.$row->id."</td>
      <td>"
.$row->nim."</td>
      <td>"
.$row->nama."</td>
      <td>"
.$row->alamat."</td>
      
     </tr>"
;
  
  }
  
 
$table .="</table>";
 
 echo 
$table


ini mysql script nya
PHP Code:
-- phpMyAdmin SQL Dump
-- version 3.4.5
-- <a target="_blank" href="http://www.phpmyadmin.net" rel="ugc" onclick="dataLayer.push({'event': 'trackEvent','eventDetails.category': 'outbond', 'eventDetails.action': 'click', 'eventDetails.label': 'http://www.phpmyadmin.net'});">http://www.phpmyadmin.net</a>
--
-- 
Hostlocalhost
-- Generation TimeJan 162013 at 09:21 AM
-- Server version5.5.16
-- PHP Version5.3.8

SET SQL_MODE
="NO_AUTO_VALUE_ON_ZERO";
SET time_zone "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- 
Database: `test`
--

-- --------------------------------------------------------

--
-- 
Table structure for table `mhs`
--

CREATE TABLE IF NOT EXISTS `mhs` (
  `
idint(11NOT NULL AUTO_INCREMENT,
  `
nimvarchar(11NOT NULL,
  `
namavarchar(20NOT NULL,
  `
alamattext NOT NULL,
  
PRIMARY KEY (`id`)
ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

--
-- 
Dumping data for table `mhs`
--

INSERT INTO `mhs` (`id`, `nim`, `nama`, `alamat`) VALUES
(1'21213''Agustian''Jakarta'),
(
5'124''Binar''Palmerah'),
(
6'345''Agung''Bekasi');

-- --------------------------------------------------------

--
-- 
Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `
idint(11NOT NULL AUTO_INCREMENT,
  `
usernamevarchar(10NOT NULL,
  `
passwordvarchar(100NOT NULL,
  
PRIMARY KEY (`id`)
ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=;

--
-- 
Dumping data for table `user`
--

INSERT INTO `user` (`id`, `username`, `password`) VALUES
(1'admin''21232f297a57a5a743894a0e4a801fc3');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */


simpan saja di satu File, kleo gak mau report misahin emoticon-Big Grinmisal di index.php

jalanin deh
Diubah oleh badycool 07-03-2013 04:05
0
9.6K
60
GuestAvatar border
Komentar yang asik ya
Mari bergabung, dapatkan informasi dan teman baru!
Programmer Forum
Programmer Forum
KASKUS Official
20.2KThread4.5KAnggota
Urutkan
Terlama
GuestAvatar border
Komentar yang asik ya
Ikuti KASKUS di
© 2024 KASKUS, PT Darta Media Indonesia. All rights reserved.