Overview

Packages

  • objSQL

Documentation

  • objSQL Connection
  • objSQL Error Handling
  • General Queries
  • Prepared Statements
  • Transactions
  • Resultset Methods
  • Helper Method - Select Query
  • Helper Method - Update Query
  • Helper Method - Insert Query
  • Helper Method - Delete Query
  • Helper Method - Paging Query
  • Helper Method - Row Count Query
  • Utility Methods
  • Firebird Notes

Classes

    cubrid_connection
  • cubrid_prepare
  • cubrid_resultset
  • cubrid_statement
  • cubrid_transaction
  • firebird_connection
  • firebird_prepare
  • firebird_resultset
  • firebird_statement
  • firebird_transaction
  • mysql_connection
  • mysql_prepare
  • mysql_resultset
  • mysql_statement
  • mysql_transaction
  • obj_access
  • objSQL
  • pdo_connection
  • pdo_prepare
  • pdo_resultset
  • pdo_statement
  • pdo_transaction
  • pgsql_connection
  • pgsql_prepare
  • pgsql_resultset
  • pgsql_statement
  • pgsql_transaction
  • sqlite3_connection
  • sqlite3_prepare
  • sqlite3_resultset
  • sqlite3_statement
  • sqlite3_transaction
  • sqlsrv_connection
  • sqlsrv_prepare
  • sqlsrv_resultset
  • sqlsrv_statement
  • sqlsrv_transaction
  • Overview
  • Package
  • Class
  • Tree
 

objSQL Helper Method: Insert

The obj_insert() helper method executes an insert statement which requires no SQL markup and is called from the objSQL class.

  • The table name argument is required.
  • The $data argument is required and MUST be a key value pair array with the table column as the key.

Returns: Result resource/object or false on failure.

See also: Inserting multiple rows

<?php

//usage: $dbh->obj_insert( $table, $data )

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
} 

?>  

Inserting Multiple Rows

The obj_insert() helper method additionally allows you to easily insert multiple rows.

  • The $data value argument can be a comma delimited string or an array.
  • SQLite3 and Firebird do not support bulk inserts.

<?php

//usage: $dbh->obj_insert( $table, array( "column" => "val1,val2,val3,val4" )

try
{
    
$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() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    echo 
$rs->obj_affected_rows();
    
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

//usage: $dbh->obj_insert( $table, array( "column" => array("val1","val2","val3","val4") )

try
{
    
$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( "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
} 

?>  

Back to top

objSQL 3.3.0 API documentation generated by ApiGen 2.8.0

Documentation licensed under a Creative Commons Attribution 3.0 Unported License.