Project Pages

PHP Classes
SourceForge
Downloads
Archived Documentation

DB Connection Methods

objSQL
obj_close

Error Handling Methods

obj_error
obj_error_message

Statement Methods

obj_delete
obj_insert
obj_paging
obj_query
obj_select
obj_update

Statement Argument Methods

obj_batch_data
obj_cols
obj_data
obj_limit
obj_offset
obj_order_by
obj_sort_order
obj_table
obj_where

Resultset Methods

obj_affected_rows
obj_fetch_assoc
obj_fetch_num
obj_fetch_object
obj_field
obj_free_result
obj_num_fields
obj_num_rows

Prepared Statement Methods

obj_bind
obj_close_statement
obj_execute
obj_free_statement
obj_prepare_statement

Transaction Methods

obj_commit
obj_rollback
obj_savepoint
obj_transaction

Utility Methods

obj_escape
obj_info
obj_row_count


obj_insert()

Description:

Executes an insert statement which requires no SQL markup and is called from the objSQL class.

  • The table name and data arguments are required unless using statement argument methods.
  • The data argument MUST be a key value pair array with the table column as the key.

See also: Batch Insert Operations


Parameters:

mixed obj_insert ( [str table[, array data ]] )


Returns:

Result resource/object or false on failure.


Example:

<?php   

try  
{  
    
$data = array( "product" => "balloon",  
                   
"color"   => "red",  
                   
"size"    => "small",  
                   
"price"   => 2.50 );  
      
    
$rs $dbh->obj_insert"mytable"$data );  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception$dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
      
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   


try   
{   
    
//set arguments using statement argument helper methods 
    
$dbh->obj_table"mytable" );  
    
$dbh->obj_data( array( "product" => "balloon",   
                           
"color"   => "red",   
                           
"size"    => "small",   
                           
"price"   => 2.50 ) );   
       
    
$rs $dbh->obj_insert();   
       
    if ( 
$dbh->obj_error() )   
        throw new 
Exception$dbh->obj_error_message() );   
           
    echo 
$rs->obj_affected_rows();   
       
}   
catch ( 
Exception $e )    
{   
    
//log error and/or redirect user to error page   
}    

?>  

See also: obj_delete, obj_paging, obj_query, obj_select, obj_update


Batch Inserts:

The obj_insert method allows you to easily perform batch insert operations.

  • The data value argument can be a comma delimited string or an array.
  • It is highly recommended to perform batch operations within a Transaction block.
<?php  

try  
{  
    
$trans $dbh->obj_transaction(); 
    
    
$data = array( "product" => "balloon,balloon,balloon,balloon",  
                   
"color"   => "red,green,blue,pink",  
                   
"size"    => "small,small,small,small",  
                   
"price"   => 2.50,2.50,2.50,2.50 );  
      
    
$rs $dbh->obj_insert"mytable"$data );  
      
    if ( 
$dbh->obj_error() )  
    {  
        
$trans->obj_rollback();  
          
        throw new 
Exception$dbh->obj_error_message() );  
    }  
          
    
$trans->obj_commit();    
    
    echo 
$rs->obj_affected_rows();  
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   


try  
{  
    
$trans $dbh->obj_transaction(); 
    
    
//set arguments using statement argument helper methods  
    
$dbh->obj_table"mytable" );   
    
$dbh->obj_data( array( "product" => array( "balloon","balloon","balloon","balloon" ),    
                           
"color"   => array( "red","green","blue","pink" ),    
                           
"size"    => array( "small","small","small","small" ),    
                           
"price"   => array( 2.50,2.50,2.50,2.50 ) ) );   
          
    
$rs $dbh->obj_insert();  
      
    if ( 
$dbh->obj_error() )  
    {  
        
$trans->obj_rollback();  
          
        throw new 
Exception$dbh->obj_error_message() );  
    }  
          
    
$trans->obj_commit();
     
    echo 
$rs->obj_affected_rows();  
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>