ORM Sample in Mysql & PHP

//
/* -=-=- database functions -=-=- */
//

function InsertData($table,$columns){
    global $wpdb;

    $sql1 = "show fields from ".$table."";
    $result1 = mysql_query($sql1) or die($sql1);
    while($res = mysql_fetch_array($result1)){
    $cols[] = $res['Field'];
    }
    array_shift($cols);
   
    // create data //
        if(is_array($cols)){
            foreach($cols as $key => $value){
                $data .= "`".$value."`='".mysql_real_escape_string($columns[$value])."' , ";
            }
        }
    // create data //
   
    $qry = "insert into ".$table." set ".$data;
    $qry = eregi_replace(', $', '', $qry);
    $result = mysql_query($qry) or die($qry);
    return($result);
}

function updateData($table,$columns,$where){
    global $wpdb;
   
    $sql1 = "show fields from ".$table."";
    $result1 = mysql_query($sql1) or die($sql1);
    while($res = mysql_fetch_array($result1)){
    $cols[] = $res['Field'];
    }
    array_shift($cols);
   
    // create data //
        if(is_array($cols)){
            foreach($cols as $key => $value){
                if(is_array($columns) and array_key_exists($value, $columns)){
                    $data .= "`".$value."`='".mysql_real_escape_string($columns[$value])."' , ";
                }
            }
        }
    // create data //
   
    $qry = "update ".$table." set ".$data;
    $qry = eregi_replace(', $', '', $qry);
    $qry = $qry." ".$where."";
    $result = mysql_query($qry) or die($qry);
    return($result);
}

function deleteData($table,$where){
        global $wpdb;
        $qry = "delete from ".$table." ".$where."";
        $result = mysql_query($qry) or die($qry);
        return($result);
    }

//
/* -=-=- database functions -=-=- */
//

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s