If we had known before its extension / MySQL and MySQLi libraries, this time we will get acquainted with the PDO, its the same thing with MySQL and MySQLi, PDO is a database access layer for PHP 5.xx version that is used to access a database from PHP, PDO provides a uniform method for accessing to multiple databases means that when we use a database supported by PDO drivers such as MySQL, Oracle, PostgreSQL, and as we just simply make it the same script using PDO, which differ only its connection string so that it will increase prodiktivitas.
To find any kind of database that is supported by PDO my friends can see -> http://www.php.net/manual/en/pdo.drivers.php. Her easy way to use this command
print_r (PDO :: getAvailableDrivers ());
if your friends are still possessed lingering questions about PDO, such as: * why should PDO PDO ?, excess ?, what the writer's String PDO how do I? And as her Please read here there, because in this paper I will not discuss it, here I cuman want to share "class Crud" that we made. Where in the Crud class there are functions insert, delete, update records. Knapa crud class is made? Her aim to readability (easy to read), modularity (split programs into modules), reusability (can be used again). I feel I have enough of her lip. Here are a class of crud that we made.
pdo.crud.class.php <- its file name
To find any kind of database that is supported by PDO my friends can see -> http://www.php.net/manual/en/pdo.drivers.php. Her easy way to use this command
print_r (PDO :: getAvailableDrivers ());
if your friends are still possessed lingering questions about PDO, such as: * why should PDO PDO ?, excess ?, what the writer's String PDO how do I? And as her Please read here there, because in this paper I will not discuss it, here I cuman want to share "class Crud" that we made. Where in the Crud class there are functions insert, delete, update records. Knapa crud class is made? Her aim to readability (easy to read), modularity (split programs into modules), reusability (can be used again). I feel I have enough of her lip. Here are a class of crud that we made.
pdo.crud.class.php <- its file name
<?php
/*
* File Name: pdo.crud.classphp
* Date: march 4, 2013
* Author: Faiz Fadly
* email : faiz_fadly@gmail.com
* Facebook : https://www.facebook.com/sourcecodeonline
* referensi:
* http://php.net/manual/en/class.pdo.php
* http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Why_use_PDO.3F
*
*/
class crud extends PDO{
private $engine;
private $host;
private $database;
private $user;
private $pass;
private $result;
public function __construct()
{
$this->engine = 'mysql';
$this->host = 'localhost';
$this->database = 'test';
$this->user = 'root';
$this->pass = '';
$dns = $this->engine.':dbname='.$this->database.";host=".$this->host;
parent::__construct( $dns, $this->user, $this->pass );
}
/*
* Insert values into the table
*/
public function insert($table,$rows=null)
{
$command = 'INSERT INTO '.$table;
$row = null; $value=null;
foreach ($rows as $key => $nilainya)
{
$row .=",".$key;
$value .=", :".$key;
}
$command .="(".substr($row,1).")";
$command .="VALUES(".substr($value,1).")";
$stmt = parent::prepare($command);
$stmt->execute($rows);
$rowcount = $stmt->rowCount();
return $rowcount;
}
/*
* Delete records from the database.
*/
public function delete($tabel,$where=null)
{
$command = 'DELETE FROM '.$tabel;
$list = Array(); $parameter = null;
foreach ($where as $key => $value)
{
$list[] = "$key = :$key";
$parameter .= ', ":'.$key.'":"'.$value.'"';
}
$command .= ' WHERE '.implode(' AND ',$list);
$json = "{".substr($parameter,1)."}";
$param = json_decode($json,true);
$query = parent::prepare($command);
$query->execute($param);
$rowcount = $query->rowCount();
return $rowcount;
}
/*
* Uddate Record
*/
public function update($tabel, $fild = null ,$where = null)
{
$update = 'UPDATE '.$tabel.' SET ';
$set=null; $value=null;
foreach($fild as $key => $values)
{
$set .= ', '.$key. ' = :'.$key;
$value .= ', ":'.$key.'":"'.$values.'"';
}
$update .= substr(trim($set),1);
$json = '{'.substr($value,1).'}';
$param = json_decode($json,true);
if($where != null)
{
$update .= ' WHERE '.$where;
}
$query = parent::prepare($update);
$query->execute($param);
$rowcount = $query->rowCount();
return $rowcount;
}
/*
* Selects information from the database.
*/
public function select($table, $rows, $where = null, $order = null, $limit= null)
{
$command = 'SELECT '.$rows.' FROM '.$table;
if($where != null)
$command .= ' WHERE '.$where;
if($order != null)
$command .= ' ORDER BY '.$order;
if($limit != null)
$command .= ' LIMIT '.$limit;
$query = parent::prepare($command);
$query->execute();
$posts = array();
while($row = $query->fetch(PDO::FETCH_ASSOC))
{
$posts[] = $row;
}
return $this->result = json_encode(array('post'=>$posts));
}
/*
* Returns the result set
*/
public function getResult()
{
return $this->result;
}
}
?>
Her live how to use the class to include php files that we created, then we intansiasikan with the NEW keyword to create an object of that class. Direct oklah example aja gw dengering dizziness not let her add ngocek not understand even more confused hehe ... following the example of its usage:
Insert.php
<?php
include "../__class/pdo.crud.class.php";
$db = new crud();
//#Insert - Contoh
$tabel = "mahasiswa";
$nama = "Syafi'i";
$data = array( 'nim' => "061250256",
'nm_mhs' => $nama,
'alamat' => "jalan ulujami Raya Gg.pancoran",
'tanggal' => "2011-03-09"
);
echo $db->insert($tabel, $data);
?>
If the return of her number 1 was the meaning of her data has been entered, if 0 failed
update.php
<?php
include "../__class/pdo.crud.class.php";
$db = new crud();
//#update - Contoh
$tabel = "mahasiswa";
$updatefild = array('nm_mhs' => "fitriyana1",
'alamat' => "fadly"
);
$where = "nim = '061250256'";
echo $db->update("mahasiswa",$updatefild, $where);
?>
Select.php
<?php
include "../__class/pdo.crud.class.php";
$db = new crud();
//#Select - Contoh
$tabel = "mahasiswa";
$fild = "*"; //menampilkan semua fild
$where = "nim='061250256'"; // tampilkan yang nim -> 0612502526
$db->select($tabel,$fild,$where);
$hasil = $db->getResult();
echo $hasil; //data di tampilkan dalam format json
?>
delete.php
<?php
include "../__class/pdo.crud.class.php";
$db = new crud();
//#Delete - Contoh
$tabel = "mahasiswa";
$where = array("nim" => "061250256");
echo $db->delete($tabel, $where);
?>
source code can be downloaded here
Hopefully Helpful, if you have questions send to faiz.fadly@gmail.com. Stay healthy to keep the spirit of coding then let bias