- Beranda
- Programmer Forum
Class PHP Simple dengan PDO (PHP Data Object)
...
![badycool](https://s.kaskus.id/user/avatar/2009/11/05/avatar1185360_2.gif)
![Avatar border](https://s.kaskus.id/images/avatarborder/1.gif)
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
, jgn yg native lagi
.Buat DB nya
Pertama kita buat Class untuk Koneksi :
Kita buat Class active record, disini ane buat untuk yg fetch Object doang, silahkan tambahkan sendiri
, Skrg kita buat handler nya (modul, apapun itu terserah ). ane contohin Class buat handle modul Mahasiswa
baru kita buat untuk tampilan datanya
ini mysql script nya
simpan saja di satu File, kleo gak mau report misahin
misal di index.php
jalanin deh
![Big Grin emoticon-Big Grin](https://s.kaskus.id/images/smilies/sumbangan/14.gif)
.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>
--
-- Host: localhost
-- Generation Time: Jan 16, 2013 at 09:21 AM
-- Server version: 5.5.16
-- PHP Version: 5.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` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nim` varchar(11) NOT NULL,
`nama` varchar(20) NOT NULL,
`alamat` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
--
-- 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` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- 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
![Big Grin emoticon-Big Grin](https://s.kaskus.id/images/smilies/sumbangan/14.gif)
jalanin deh
Diubah oleh badycool 07-03-2013 04:05
0
9.6K
60
![Guest](https://s.kaskus.id/user/avatar/default.png)
![Avatar border](https://s.kaskus.id/images/avatarborder/1.gif)
Komentar yang asik ya
Mari bergabung, dapatkan informasi dan teman baru!
![Programmer Forum](https://s.kaskus.id/r200x200/ficon/image-176.png)
Programmer Forum![KASKUS Official KASKUS Official](https://s.kaskus.id/kaskus-next/next-assets/images/icon-official-badge.svg)
20.2KThread•4.5KAnggota
Urutkan
Terlama
![Guest](https://s.kaskus.id/user/avatar/default.png)
![Avatar border](https://s.kaskus.id/images/avatarborder/1.gif)
Komentar yang asik ya