PDA

View Full Version : کلاس mysql



T.R.G.T
دوشنبه 14 اسفند 1391, 10:11 صبح
دو سه ماه دیگه که یه کلاس دیگه نوشتم نسخه آپدیت شده رو قرار میدم

rezaonline.net
دوشنبه 14 اسفند 1391, 10:38 صبح
سلام
معذرت میخوام از نظر فنی چون معمولا یک دیتابیس داریم و چندیدن جدول ، پس باید کلاس رو جوری پیاده کنیم که فقط یک شی ازش بگیریم و بقیه متدها رو درخواست کنیم و ...
الان توی همین مثال شما به ازای هر دستور یک شی گرفته میشه که این مصرف حافظه زیادی داره و اصلا کار درستی نیست .
بهتره یک بازنگری کنید .
پیشنهادم اینه متدهایی بعنوان insert , update, delete, و ... ایجاد کنید ، در ابتدای هر متد اتصال به دیتابیس رو چک کنید و بقیه کارها رو انجام دهید .
امیدوارم با پیشرفت کلاستون ، شاهد یک کلاس دیتابیس حرفه ای باشیم .
موفق باشید

T.R.G.T
دوشنبه 14 اسفند 1391, 10:48 صبح
یک کلاس برای mysql نوشته بودم آپدیتش کردم و نسخه قدیمیش رو اینجا میزارم تا استفاده کنید
امکانات
درج داده
حذف داده
ویرایش داده
درج جدول
حذف جدول
استفاده ازش خیلی سادس کافیه کلاس رو فراخوانی کنید ودستور رو بهش بدید مانند مثال زیر


$test=new mysql('read','table','column','order*','group*','w here*');

$test=new mysql('insert','column','value');

$test=new mysql('update','table','column','where*','value');

$test=new mysql('delete','table','where*');

$test=new mysql('add_table','table','table_column');

$test=new mysql('delete_table','table');

$test=new mysql('query','query');

گزینه های ستاره دار اختیاری هستند ومیتونید اونا رو خالی بزارید ولی حتما باید مقدار دهیشون بکنید
مقدار اولی که به کلاس میدید دستوریه که میخواید اجرا بکنه که به ترتیب
read
برای خواندن داده
insert
برای نوشتن
delete
برای حذف
update
برای ویرایش
delete_table
برای حذف جدول
add_table
برای افزودن جدول
query
برای فرستادن یک دستور



<?php
class mysql{
protected $host;
protected $user;
protected $password;
protected $database;
protected $connection;
public $table;
public $column;
public $order;
public $group;
public $where;
public $value;
public $table_column;
public $query;
function mysql(){
$config=new config;
$this->host=$config->config_sistem_file('mysql_host');
$this->database=$config->config_sistem_file('mysql_db');
$this->user=$config->config_sistem_file('mysql_user');
$this->password=$config->config_sistem_file('mysql_password');
$ar_ix=func_num_args();
$ar_ar=func_get_args();
if($ar_ix>0){
$order=$ar_ar[0];
$this->connection=$this->connect();
if($order=='read'){
$this->table=$ar_ar[1];
$this->column=$ar_ar[2];
$this->order=$ar_ar[3];
$this->group=$ar_ar[4];
$this->where=$ar_ar[5];
return $this->read();
}elseif($order=='insert'){
$this->table=$ar_ar[1];
$this->column=$ar_ar[2];
$this->value=$ar_ar[3];
return $this->insert();
}elseif($order=='update'){
$this->table=$ar_ar[1];
$this->column=$ar_ar[2];
$this->where=$ar_ar[3];
$this->value=$ar_ar[4];
return $this->update();
}elseif($order=='dalete'){
$this->table=$ar_ar[1];
$this->column=$ar_ar[2];
$this->where=$ar_ar[3];
return $this->delete();
}elseif($order=='add_table'){
$this->table=$ar_ar[1];
$this->table_column=$ar_ar[2];
return $this->delete();
}elseif($order=='delete_table'){
$this->table=$ar_ar[1];
return $this->delete();
}elseif($order=='query'){
$this->query=$ar_ar[1];
return $this->query();
}
}
}
protected function connect(){
$conct=mysql_connect($this->host,$this->user,$this->password) or die('Error In Connect<br />'.mysql_error());
mysql_select_db($this->database,$conct) or die('Error In Select Database<br />'.mysql_error());
return $conct;

}
public function close_connection(){
mysql_close($this->connection);
}
public function read(){
if(isset($this->where) and !empty($this->where)){
$where_set=' where '.$this->where;
}else{
$where_set='';
}
if(isset($this->order) and !empty($this->order)){
$order_set=' ORDER BY '.$this->order.' ';
}else{
$order_set='';
}
if(isset($this->group) and !empty($this->group)){
$group_set=' GROUP BY '.$this->group.' ';
}else{
$group_set='';
}
$query=mysql_query('select '.$this->column.' from '.$order_set.$this->table.$group_set.$where_set,$this->connection)or die('Error In Read<br/>'.mysql_error());
return $query;
}
public function insert(){
$query=mysql_query('insert into '.$this->table.' ('.$this->column.') values ('.$this->value.');',$this->connection)or die('Error In Insert<br/>'.mysql_error());
return $query;
}
public function update(){
if(isset($this->where) and !empty($this->where)){
$where_set=' where '.$this->where;
}else{
$where_set='';
}
$query=mysql_query('update '.$this->table.' set '.$this->column.' = '.$this->value.$where_set,$this->connection)or die('Error In Update<br/>'.mysql_error());
return $query;
}
public function delete(){
if(isset($this->where) and !empty($this->where)){
$where_set=' where '.$this->where;
}else{
$where_set='';
}
$query=mysql_query('delete from '.$this->table.$where_set,$this->connection)or die('Error In Update<br/>'.mysql_error());
return $query;
}
public function create_table(){
$query=mysql_query('CREATE TABLE '.$this->table.'('.$this->table_column.')',$this->connection);
return $query;
}
public function delete_table(){
$query=mysql_query('DROP TABLE \''.$this->table.'\'',$this->connection);
return $query;
}
public function query(){
$query=mysql_query($this->query,$this->connection);
return $query;
}
}
?>


بهتره با دقت بیشتر بخونید ممنون از نظرتون

AbiriAmir
دوشنبه 14 اسفند 1391, 13:47 عصر
خودتون هم میدونید استاندارد نیست و آپدیتش کردید بعد قدیمیش رو گذاشتید اینجا که ما استفاده کنیم؟!؟! :گیج: