تابع موضوعات المنتدى الانتساب تعليمات س - ج قائمة الأعضاء المفكرة بحث مواضيع اليوم إشارة الأقسام مقروءة

مواقع صديقة: تعلم البي اتش بي - تصميم نت - أفضل استضافة

قديم 29-09-2008, 01:41 AM   #1 (permalink)
عضو جديد
 
تاريخ الانتساب: 29-09-2008
مشاركات: 3
مستوى السمعة : 0
محمد الأنصاري على طريق التميز و النجاح
Lightbulb مجموعة أصناف إدارة الجداول في قاعدة البيانات MySQL (أتمني أن تجربوه و تكتشفو مع عيوبه)

تتكون هذه المجموعة من صنفين الأول هو ملف mysql_table.php و هذا الملف هو الملف الرئيسي الذي يجب أن تضعه في صفحة البي إتش بي التي تريد أستخدامه فيه أما الملف الثاني condition_set.php فهو يستخدم مع الملف الأول و لا تحتاج لإضافته (الانه مضاف في الملف الأول ) :
و في ما يلي نص الملف الأول mysql_table.php :
كود PHP:
<?php
include_once("condition_set.php");

class 
mysql_table{
    
    var 
$name="";
    var 
$mysql_config;
    var 
$fields="*";
    
    var 
$group_by=false;
    var 
$group_by_asc=true;
    
    var 
$order_by=false;
    var 
$order_by_asc=true;
    
    var 
$limit_position=false;
    var 
$limit_size=false;
    var 
$conditions_set=false;
    
    var 
$total_row=0;
    var 
$total_pages=0;
    
    var 
$last_insert_id=0;
    
    var 
$update_with_condition_only=true;
    var 
$delete_with_condition_only=true;
    
    
    function 
mysql_table($name,$config=false){
        if(
$config){
            
$this->mysql_config=$config;
        }else {
            
$this->mysql_config["host"]="localhost";
            
$this->mysql_config["user"]="user";
            
$this->mysql_config["pass"]="pass";
            
$this->mysql_config["database"]="db";            
        }
        
$this->name=$name;
    }
    function 
set_table($table){
        
$this->mysql_config["table"]="$table";
    }
    function 
select($fields,$order_by=false,$limit_position=false,$limit_size=false){
        
$this->fields=$fields;
        
        if(
$order_by){
            
$this->order_by=$order_by;    
        }
        if(
$limit_size){
            
$this->limit_size=$limit_size;    
        }
        if(
$limit_position){
            
$this->limit_position=$limit_position;    
        }
    }
    function 
where($condition_set){
        
$this->conditions_set=$condition_set;
    }
    function 
get_array(){
        if (!
$link mysql_connect($this->mysql_config["host"], $this->mysql_config["user"], $this->mysql_config["pass"])) {
            echo 
'Could not connect to mysql';
            exit;
        }
        
        if (!
mysql_select_db($this->mysql_config["database"], $link)) {
            echo 
'Could not select database';
            exit;
        }
        
        
$sql_without_limit=$this->sql(false);
        
$result_without_limit=mysql_query($sql_without_limit$link);
        
$this->total_row=mysql_num_rows($result_without_limit);
        
$this->total_pages=ceil($this->total_row/$this->limit_size);
        
        
$sql    =$this->sql();
        
$result mysql_query($sql$link);
        
        
        
        if (!
$result) {
            echo 
"DB Error, could not query the database\n";
            echo 
'MySQL Error: ' mysql_error();
            exit;
        }
        
//Test End
        
while ($row mysql_fetch_assoc($result)) {
            
$result_array[]=$row;
        }
        
        
mysql_free_result($result);
        
mysql_free_result($result_without_limit);
        return 
$result_array;
    }
    function 
insert($fields_array){
        if (!
$link mysql_connect($this->mysql_config["host"], $this->mysql_config["user"], $this->mysql_config["pass"])) {
            echo 
'Could not connect to mysql';
            exit;
        }
        
        if (!
mysql_select_db($this->mysql_config["database"], $link)) {
            echo 
'Could not select database';
            exit;
        }
        
$fields=array_keys($fields_array);
        
$values=array_values($fields_array);
        
$sql="Insert into ".$this->name."(".implode(",",$fields).") VALUES('".implode("','",$values)."');";
        
        
mysql_query($sql);
        
//mysql_query()
        
$this->last_insert_idmysql_insert_id($link);
    }
    function 
update($fields_array){
        if(
$this->update_with_condition_only){
            if(!
is_object($this->conditions_set)){
                exit();
            }
        }
        if (!
$link mysql_connect($this->mysql_config["host"], $this->mysql_config["user"], $this->mysql_config["pass"])) {
            echo 
'Could not connect to mysql';
            exit;
        }
        if (!
mysql_select_db($this->mysql_config["database"], $link)) {
            echo 
'Could not select database';
            exit;
        }
        
$sql="UPDATE ".$this->name." SET ";
        
$first=true;
        foreach (
$fields_array as $key=>$value) {
            if(
$first){
                
$sql .=" ".$key."='".$value."'";
                
$first=false;
            }else{
                
$sql .=" ,".$key."='".$value."'";
            }
        }
        if(
is_object($this->conditions_set)){
            
//Append Conditions
            
$sql .=$this->conditions_set->result();
        }
        
mysql_query($sql);
    }
    function 
delete(){
        if(
$this->delete_with_condition_only){
            if(!
is_object($this->conditions_set)){
                exit();
            }
        }
        if (!
$link mysql_connect($this->mysql_config["host"], $this->mysql_config["user"], $this->mysql_config["pass"])) {
            echo 
'Could not connect to mysql';
            exit;
        }
        if (!
mysql_select_db($this->mysql_config["database"], $link)) {
            echo 
'Could not select database';
            exit;
        }
        
$sql="DELETE FROM ".$this->name." ";
        if(
is_object($this->conditions_set)){
            
//Append Conditions
            
$sql .=$this->conditions_set->result();
        }
        
        
mysql_query($sql);
    }
    function 
sql($include_limit=true){
        
//Generate SQL
        
$sql="SELECT ".$this->fields." FROM ".$this->name;
        
//look for conditions
        
if(is_object($this->conditions_set)){
            
//Append Conditions
            
$sql .=$this->conditions_set->result();
        }
        if(
$this->group_by){
            
//Append Conditions
            
$sql .=" Group By ".$this->group_by;
            if(!
$group_by_asc){
                
$sql .=" DESC";
            }
        }
        if(
$this->order_by){
            
//Append Conditions
            
$sql .=" Order By ".$this->order_by;
            if(!
$this->order_by_asc){
                
$sql .=" DESC";
            }
        }
        if(
$this->limit_size && $this->limit_position && $include_limit){
            
$sql .=" Limit ".(($this->limit_position-1)*$this->limit_size).",".$this->limit_size;
        }
        return 
$sql;
    }

}

?>
الملف الثاني condition_set.php

كود PHP:
<?php
class condition_set{
    
    var 
$conditions;
    var 
$or_conditions;
    function 
like($field_name,$value,$not=false){
        
$cond=$field_name." Like '".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
equal($field_name,$value,$not=false){
        
$cond=$field_name."='".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }    
    }
    function 
greater_than($field_name,$value,$not=false){
        
$cond=$field_name." >'".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
less_than($field_name,$value,$not=false){
        
$cond=$field_name." <'".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
equal_or_less($field_name,$value,$not=false){
        
$cond=$field_name." <='".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
equal_or_greater($field_name,$value,$not=false){
        
$cond=$field_name." >='".$value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
between($field_name,$fisrt_value,$second_value,$not=false){
        
$cond=$field_name." BETWEEN '".$fisrt_value."' AND '".$second_value."'";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    function 
in($field_name,$value_array,$not=false){
        
$cond=$field_name." in('".implode("','",$value_array)."')";
        if(
$not){
            
//true
            
$this->conditions[]="Not ".$cond;
        }else{
            
$this->conditions[]=$cond;    
        }
    }
    
    function 
result(){
        if(
is_array($this->or_conditions)){
            if(
is_array($this->conditions))    {
                
$this->or_conditions[]=implode(" AND ",$this->conditions);
                unset(
$this->conditions);
            }
            return  
" WHERE ".implode(" Or ",$this->or_conditions);
        }else{
            if(
is_array($this->conditions))    {
                return  
" WHERE ".implode(" AND ",$this->conditions)    ;
            }            
        }

    }
    function 
reset(){
        unset(
$this->conditions);
    }
    function 
start_new_condition_set(){
        
//Separate
        
$this->or_conditions[]=implode(" AND ",$this->conditions);
        unset(
$this->conditions);
    }
}

?>
طبعا لكل كود فلسفة خاصة و من أهم فلسفة هذا الكود :
  • أختصار الكثير من كتابة الكود
  • التوافقية مع معظم سناريوهات الإستخدام الشائعة
  • إمكانية كتابة أكثر تعليمات لإسكيول تعقيدا بعدة أسطر

سناريوهات الإستخدام الشائعة ..
التأكد من كلمة المرور و إسم المستخدم


كود PHP:
<?php
include_once("mysql_table.php");

$user=new mysql_table("users");
$cond=new condition_set();
$cond->equal("username","محمد الأنصاري");
$cond->equal("password","إفتح ياسمسم");
$user->where($cond);

$this_user=$user->get_array();

if(
$user->total_row>0){
    echo 
"اهلا و سهلا بك لقد تم تسجيل دخولك";
}else{
    echo 
"أسم المستخدم أو كلمة المرور غير صحيحة";
}

?>
إضافة سجل جديد إلى جدول المنتجات :
كود PHP:
<?php
include_once("mysql_table.php");

$products=new mysql_table("product");

$prouct_one["name"]="Apple";
$prouct_one["price"]=3;

$products->insert($prouct_one);
تعديل سجل من جدول الموظفين :
كود PHP:
<?php
include_once("mysql_table.php");

$employees=new mysql_table("employees");

$cond=new condition_set();
$cond->equal("id","1");
$cond->where($cond);

$data["fullname"]="محمد الأنصاري";
$data["salary"]=3500;

$employees->update($data);


?>
حذف سجل من من جدول الموظفين وفق شروط معينة :

كود PHP:
<?php
include_once("mysql_table.php");

$employees=new mysql_table("employees");

$cond=new condition_set();
$cond->equal("id","1");
$cond->where($cond);

$employees->delete();


?>
هذه من السناريوهات المتكررة و سوف أعرض عدة سناريوهات جديدة في مشاركات قادمة مع العلم أنه يمكنكم كتابة سؤال حول احد سناريوهات الإستخدام و سأشرح طريقة أستخدمه إذا كان ذلك ممكنا ...

مع تحياتي للجميع
محمد الأنصاري غير متصل   رد مع اقتباس
إضافة رد


زوار هذا الموضوع الآن : 1 (0 عضو و 1 ضيف)
 

(View-All Members who have read this thread : 18
ملاك الشوق, مبتدئ فى البرمجه, محمد الأنصاري, محمدهلال, أيمن, ميت احساس, alexsniper, amirdz, aroon, المبهر, الشقى المؤدب, بلال كيالي, man in dark, man summits, najdsky, p!a_md5, Sa3Q, غزوان
أدوات الموضوع
طريقة العرض
عادي عادي

ضوابط المشاركة
غير مصرّح لك بنشر موضوع جديد
غير مصرّح لك بنشر ردود
غير مصرّح لك برفع مرفقات
غير مصرّح لك بتعديل مشاركاتك

وسوم vB : نشيط
كود [IMG] : نشيط
كود هتمل : خامل
Trackbacks are نشيط
Pingbacks are نشيط
Refbacks are نشيط
الانتقال السريع

مواضيع ذات صلة
الموضوع الكاتب المجلس المشاركات المشاركة الأخيرة
كيف انشا قاعدة mysql في php aladine أسئلة واستفسارات PHP Questions 2 26-08-2008 03:28 PM
مشكلة تحويل قاعدة بيانات اكسس الى mysql أصيل2008 مشاكل وحلول PHP Errors & Troubleshooting 0 11-07-2008 02:39 AM
تحويل قاعدة xlsx الى mysql ؟ GHOST مشاكل وحلول PHP Errors & Troubleshooting 0 29-04-2008 03:49 PM
لماذا لا تستجيب قاعدة بيانات mysql HUM100 مشاكل وحلول PHP Errors & Troubleshooting 0 11-03-2008 08:07 PM
مجموعة أسئلة في الـ php & mysql محتاج لتوضيحها هكرز المنتديات مشاكل وحلول PHP Errors & Troubleshooting 9 09-10-2007 03:00 AM


يمنع نقل أي من موضوعات المنتدى دون إذن كتابي مسبق من إدارة المنتدى، مع الشكر.
التوقيت حسب جرينتش +3. الساعة الآن 04:44 PM.