Kişisel bir proje için yazdığım veritabanı sınıfım;
<?php
class db
{
private $server = ''; // Mysql location
private $user = ''; // Mysql user name
private $pass = '' ; // Mysql password
private $db = ''; // Mysql database name
private $connect; // Connection varible
function __construct()
{
$this->connect = mysql_connect($this->server,$this->user,$this->pass);
if($this->connect){
mysql_select_db($this->db);
return true;
}else{
return false;
}
}
function query($query)
{
$run = mysql_query($query,$this->connect);
if($run){
return true;
}else{
return false;
}
}
function turn($veriable)
{
if($veriable){
return true;
}else{
return false;
}
}
function count($data,$operator)
{
$count = count($data);
if($count > 1){
foreach ($data as $key => $value){
$where[] = $key.' = "'.$value.'"';
}
return $where = implode(' '.$operator.' ' , $where);
}else{
$keys = array_keys($data);
$values = array_values($data);
return $where = $keys[0] .' = "'.$values[0].'"';
}
}
function insert(array $data ,$table)
{
if(count($data) > 1){
$datas = implode(',',array_keys($data));
$values = implode('","',array_values($data));
}else{
foreach ($data as $dataa => $valuee){
$datas = $dataa;
$values = $valuee;
}
}
$insert = $this->query('INSERT INTO '.$table.' ('.$datas.') VALUES ("'.$values.'")');
return $this->turn($insert);
}
function delete(array $data ,$table)
{
$where = $this->count($data,'AND');
$delete = $this->query('DELETE FROM '.$table.' WHERE '.$where.'');
return $this->turn($delete);
}
function update(array $data ,array $where, $table)
{
$set = $this->count($data,',');
$where = $this->count($where,'AND');
$update = $this->query('UPDATE '.$table.' SET '.$set.' WHERE '.$where.' ');
return $this->turn($update);
}
function __destruct()
{
mysql_close();
}
}
?>Kullanımı;
<?php
include('db.php');
$db = new db;
$db->insert(array('hücre' => 'veri'), 'veritabani'); // Veri ekle
$db->update(array('hücre' => 'veri'),array('değişecek hücre' => 'değişecek veri'), 'veritabani'); // Veri güncellle
$db->delete(array('hücre' => 'veri'), 'veritabani'); //Veri sil
?>