omerabul
kurumsal
kendi özel kodladığım pdo db classı... php ile kodladığım bir çok projemde bu classı kullanmaktayim..
construct kısmını kendinize göre ayarlayabilirsin. ben genelikle config.php icinde
tarzında kullanıyorum...
PHP:
<?php
class dbHelper {
private $pdo;
function __construct() {
global $settings;
try {
$this->pdo = new PDO("mysql:host={$settings["server"]};
dbname={$settings["db_name"]}", "{$settings["user"]}", "{$settings["password"]}",
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::MYSQL_ATTR_FOUND_ROWS => true
)
);
} catch(PDOException $ex) {
die('Veritabani Baglantisi Saglanamadi... <br />Hata: ' . $ex -> getMessage());
}
}
public function delete($table, $where, $id) {
try {
$data = array($id);
$sel = $this -> pdo -> prepare("Delete from $table where $where=?");
$sel -> execute($data);
return array("success" => true, "msg" => ("Silindi..."));
} catch(PDOException $x)
{
return array("success" => false, "msg" => $x->getMessage());
}
}
public function update($table, $dat, $id, $val) {
if ($dat !== null)
$data = array_values($dat);
array_push($data, $val);
//grab keys
$cols = array_keys($dat);
$mark = array();
foreach ($cols as $col) {
$mark[] = $col . "=?";
}
$im = implode(', ', $mark);
$ins = $this -> pdo -> prepare("UPDATE $table SET $im where $id=?");
$ins -> execute($data);
}
public function update2($table, $data, $where=array()) {
if(!empty($where)) {
$whereOutput = implode(' AND ', array_map(
function ($v, $k) { return sprintf("%s=:%s", $k, $k); },
$where,
array_keys($where)
));
}
$dataOutput = implode(', ', array_map(
function ($v, $k) { return sprintf("%s=:%s", $k, $k); },
$data,
array_keys($data)
));
$data = array_merge($data,$where);
foreach($data as $k => $v) {
$data[":".$k] = trim($v);
unset($data[$k]);
}
$sqlText= "UPDATE $table SET $dataOutput ";
if(!empty($where)) {
$sqlText .=" WHERE $whereOutput ";
}
// $result = array("sql" => $sqlText, "data" => $data);
$ins = $this -> pdo -> prepare($sqlText);
$ins -> execute($data);
return $ins->rowCount() >0 ? true : false ;
}
public function delete2($table, $where=array()) {
if(!empty($where)) {
$whereOutput = implode(' AND ', array_map(
function ($v, $k) { return sprintf("%s=:%s", $k, $k); },
$where,
array_keys($where)
));
}
$data = $where;
foreach($data as $k => $v) {
$data[":".$k] = $v;
unset($data[$k]);
}
$sqlText= "DELETE FROM $table";
if(!empty($where)) {
$sqlText .=" WHERE $whereOutput ";
}
$result = array("sql" => $sqlText, "data" => $data);
$ins = $this -> pdo -> prepare($sqlText);
$ins -> execute($data);
return $ins->rowCount() >0 ? true : false ;
}
public function get_all($table, $where=array(),$type="array") {
if(!empty($where)) {
$whereOutput = implode(' AND ', array_map(
function ($v, $k) { return sprintf("%s=:%s", $k, $k); },
$where,
array_keys($where)
));
}
$data = $where;
foreach($data as $k => $v) {
$data[":".$k] = $v;
unset($data[$k]);
}
$sqlText= "SELECT * FROM $table";
if(!empty($where)) {
$sqlText .=" WHERE $whereOutput ";
}
//return $result = array("sql" => $sqlText, "data" => $data);
$ins = $this -> pdo -> prepare($sqlText);
$ins -> execute($data);
if($type=="array") {
return $ins->fetchAll(PDO::FETCH_ASSOC);
}
if($type=="class") {
return $ins->fetchAll(PDO::FETCH_CLASS);
}
}
public function get($table, $where=array(),$type="array") {
if(!empty($where)) {
$whereOutput = implode(' AND ', array_map(
function ($v, $k) { return sprintf("%s=:%s", $k, $k); },
$where,
array_keys($where)
));
}
$data = $where;
foreach($data as $k => $v) {
$data[":".$k] = $v;
unset($data[$k]);
}
$sqlText= "SELECT * FROM $table";
if(!empty($where)) {
$sqlText .=" WHERE $whereOutput ";
}
//return $result = array("sql" => $sqlText, "data" => $data);
$ins = $this -> pdo -> prepare($sqlText);
$ins -> execute($data);
if($type=="array") {
return $ins->fetch(PDO::FETCH_ASSOC);
}
if($type=="class") {
return $ins->fetch(PDO::FETCH_CLASS);
}
}
public function insert($table, $dat) {
if ($dat !== null)
$data = array_values($dat);
//grab keys
$cols = array_keys($dat);
$col = implode(', ', $cols);
//grab values and change it value
$mark = array();
foreach ($data as $key) {
$keys = '?';
$mark[] = trim($keys);
}
$im = implode(', ', $mark);
$ins = $this -> pdo -> prepare("INSERT INTO $table ($col) values ($im)");
$ins -> execute($data);
return $this->pdo -> lastInsertId();
}
public function fetch_all($table)
{
$sel = $this->pdo->prepare("SELECT * FROM $table");
$sel->execute();
return $sel->fetchAll(PDO::FETCH_ASSOC);
}
public function fetch($table)
{
$sel = $this->pdo->prepare("SELECT * FROM $table");
$sel->execute();
return $sel->fetch(PDO::FETCH_ASSOC);
}
public function query($sorgu)
{
$sel = $this->pdo->query($sorgu);
return $sel;
}
public function prepare($sorgu)
{
$sel = $this->pdo->prepare($sorgu);
return $sel;
}
public function count($table)
{
$sel = $this->pdo->query("Select Count(*) FROM $table");
return $sel->fetchColumn();
}
public function __destruct() {
$this -> pdo = null;
}
}
?>
construct kısmını kendinize göre ayarlayabilirsin. ben genelikle config.php icinde
PHP:
$settings = array(
"server" => "localhost",
"user" => "root",
"password" =>"",
"db_name" => "cafe"
);
include "dbHelper.php";
$db = new dbHelper();
$veriler = $db->get_all( array("KategoriID" => 5, "MarkaID" => 5) );