DB Abstraction

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
vadim88
Forum Newbie
Posts: 3
Joined: Mon Feb 04, 2008 9:40 am

DB Abstraction

Post by vadim88 »

Hey,

i use a file with all my queries but i want to make it even better by making a file called mysql.php which will consist of all the DB select/update/delete/insert function so instead of using in every qurey:

Code: Select all

$result = mysql_query("SELECT * FROM person");while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />"
  }
I will put in the mysql.php file a function that called 'select' then i will use the function in everyfile i want as this:

Code: Select all

include("mysql.php");
$result = $DB->select("SELECT * FROM person");
while($row = $DB->fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />"
  }
this is an example of just the select function but i want to do it to select/update/delete/insert so there is no complications.

Im sure im making myself clear but does anyone have something like that already coded i would like taking alook at it.

here is an example of IPS mysql DB abstraction:

Code: Select all

* Update:
* $db->do_update( 'table', array( 'field' => 'value', 'field2' => 'value2' ), 'id=1' );
* Insert
* $db->do_insert( 'table', array( 'field' => 'value', 'field2' => 'value2' ) );
 
    /*-------------------------------------------------------------------------*/
    // Quick function: DO UPDATE
    /*-------------------------------------------------------------------------*/
    
    function do_update( $tbl, $arr, $where="", $shutdown=FALSE )
    {
        //-----------------------------------------
        // Form query
        //-----------------------------------------
        
        $dba   = $this->compile_db_update_string( $arr );
        $query = "UPDATE ".$this->obj['sql_tbl_prefix']."$tbl SET $dba";
        
        if ( $where )
        {
            $query .= " WHERE ".$where;
        }
        
        //-----------------------------------------
        // Shut down query?
        //-----------------------------------------
        
        $this->no_prefix_convert = 1;
        
        if ( $shutdown )
        {
            if ( ! $this->obj['use_shutdown'] )
            {
                $this->is_shutdown = 1;
                $return = $this->query( $query );
                $this->no_prefix_convert = 0;
                $this->is_shutdown = 0;
                return $return;
            }
            else
            {
                $this->obj['shutdown_queries'][] = $query;
                $this->no_prefix_convert = 0;
                $this->cur_query = "";
            }
        }
        else
        {
            $return = $this->query( $query );
            $this->no_prefix_convert = 0;
            return $return;
        }
    }
    /*-------------------------------------------------------------------------*/
    // Quick function: DO INSERT
    /*-------------------------------------------------------------------------*/
    
    function do_insert( $tbl, $arr, $shutdown=FALSE )
    {
        //-----------------------------------------
        // Form query
        //-----------------------------------------
        
        $dba   = $this->compile_db_insert_string( $arr );
        $query = "INSERT INTO ".$this->obj['sql_tbl_prefix']."$tbl ({$dba['FIELD_NAMES']}) VALUES({$dba['FIELD_VALUES']})";
        
        //-----------------------------------------
        // Shut down query?
        //-----------------------------------------
        
        $this->no_prefix_convert = 1;
        
        if ( $shutdown )
        {
            if ( ! $this->obj['use_shutdown'] )
            {
                $this->is_shutdown = 1;
                $return = $this->query( $query );
                $this->no_prefix_convert = 0;
                $this->is_shutdown = 0;
                return $return;
            }
            else
            {
                $this->obj['shutdown_queries'][] = $query;
                $this->no_prefix_convert = 0;
                $this->cur_query = "";
            }
        }
        else
        {
            $return = $this->query( $query );
            $this->no_prefix_convert = 0;
            return $return;
        }
    }
something like that, of course no need for all that something simple that will make the job.

would appreciate any help, thanks vadim.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: DB Abstraction

Post by Christopher »

You should search these forums for something like "DB Connection Class" or look around the Design and Theory forum about Models.
(#10850)
vadim88
Forum Newbie
Posts: 3
Joined: Mon Feb 04, 2008 9:40 am

Re: DB Abstraction

Post by vadim88 »

thanks figured it out, thanks for the help arborint problem is solved.
Post Reply