Jump to content

DB Abstraction


vadim88

Recommended Posts

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:

 

$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:

 

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:

 

* 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.

Link to comment
Share on other sites

i want a code that will i would use this:

 

$db->do_insert( 'table', array( 'field' => 'value', 'field2' => 'value2' ) );

 

instead of the whole insert function in order to insert something to the database, i think its called DB abstraction, i posted an example in the last code box .

Link to comment
Share on other sites

help with it, i dont really know how to do that, i dont ask u to write the whole thing just if it's possible to write at least the select function thing so i could see how it works and try to write down the others by myself, there is no tutorial nor and kind of article that explains how this can be done.

 

thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.