objSQL 3.9.0 Documentation


objSQL()

Description:

Opens a new connection to a named database server.

Parameters:

Comma delimited string

mixed objSQL ( str driver, str host, str user, str pass, str database[, int port ] )

Array

mixed objSQL ( array ( str driver, str host, str user, str pass, str database[, int port ] ) )

Returns:

Connection resource or object.

Example:

<?php  

/**  

PHP 5.4/5.5 Drivers  

CUBRID: cubrid 
Firebird: firebird 
MariaDB: mariadb or mysql  
MySQL: mysql 
Oracle: oracle 
Postgres: pgsql 
SQLite3: sqlite 
SQL Server: sqlsrv 

PHP 5.6 Drivers  

CUBRID: cubrid 
Firebird: firebird 
MariaDB: mariadb or mysql  
MySQL: mysql 
Oracle: oracle 
Postgres: pgsql 
SQLite3: sqlite 
SQL Server: sqlsrv 

PHP 7 Drivers  

Firebird: firebird 
MariaDB: mariadb or mysql  
MySQL: mysql 
Oracle: oracle 
Postgres: pgsql 
SQLite3: sqlite 
SQL Server: sqlsrv 

*/  

try  
{  
    
$dbh = new objSQL( "mysql,localhost,root,pass,mydb,3306" );  
          
    if ( 
error_get_last() !== null )  
        throw new 
Exception( error_get_last()['message'] );  
          
    
//Query block    
      
    
$dbh->obj_close();      
}  
catch ( 
Exception $e )   
{  
    echo 
$e->getMessage();  
}  


try  
{  
    
$dbh = new objSQL( array( "mysql","localhost","root","pass","mydb",3306 ) );  
          
    if ( 
error_get_last() !== null )  
        throw new 
Exception( error_get_last()['message'] );  
          
    
//Query block    
      
    
$dbh->obj_close();      
}  
catch ( 
Exception $e )   
{  
    echo 
$e->getMessage();  
}  

?>

See also: obj_close

Back To Top

obj_close()

Description:

Closes connection to a named database server.

Parameters:

bool obj_close ( void )

Returns:

True on success or false on failure.

Example:

<?php

try
{
    
$dbh = new objSQL( "mysql,localhost,root,pass,mydb,3306" );
        
    if ( 
error_get_last() !== null )
        throw new 
Exception( error_get_last()['message'] );
        
    
//Query block  
    
    
$dbh->obj_close();    
}
catch ( 
Exception $e ) 
{
    echo 
$e->getMessage();
}

?>   

See also: objSQL

Back To Top

obj_error()

Description:

Error flag for the last error.

Parameters:

bool obj_error ( void )

Returns:

True on last error or false if no error.

Example:

<?php

try
{
    
$rs = $dbh->obj_query( "delete from mytable where id=1" );
        
    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_error_message

Back To Top

obj_error_message()

Description:

String description of the last error.


Parameters:

mixed obj_error_message ( void )


Returns:

Verbose error message or null if no error.


Example:

<?php

try
{
    
$rs = $dbh->obj_query( "delete from mytable where id=1" );
        
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
           
    echo 
$rs->obj_affected_rows();    
        
    
$dbh->obj_close();  
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
}

?>   

See also: obj_error

Back To Top

obj_delete()

Description:

Executes a delete statement with minimal SQL markup and is called from the objSQL class.

Parameters:

mixed obj_delete ( [str table[, str where ]] )

Returns:

Result resource/object or false on failure.

Example:

<?php 

try 
{ 
    
$rs = $dbh->obj_delete( "mytable", "dept='IT'" ); 
     
    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_where( "dept='IT'" );
    
    
$rs = $dbh->obj_delete(); 
     
    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_insert, obj_paging, obj_query, obj_select, obj_update

Back To Top

Batch Delete Example:

<?php     

try    
{    
    
$trans = $dbh->obj_transaction();   
     
    
//data argument can be an array or comma delimited string 
    //value(s) should be a unique field in a row (ie: primary key, part #, mfg product #, etc. )  
    
$data = array( 1234,2345,3456,4567,5678,6789 ); 
    
$data = "1234,2345,3456,4567,5678,6789"; 
     
    
//the second argument is the column name for the unique field 
    
$dbh->obj_batch_data( $data, "employee_id" );   
     
    
$dbh->obj_table( "employees" );   
            
    
$rs = $dbh->obj_delete();    
        
    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    
}     

?>   

See also: obj_batch_data, obj_insert, obj_paging, obj_query, obj_select, obj_update

Back To Top

obj_insert()

Description:

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

See also: Inserting Multiple Rows

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

Inserting Multiple Rows:

The obj_insert method allows you to easily insert multiple rows.

<?php 

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 
}  


try 
{ 
    
//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() ) 
        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

obj_paging()

Description:

The obj_paging method allows you to facilitate recordset paging queries with minimal SQL markup and is called from the objSQL class. Common uses include online catalogs where the user can view multi-page results by specifying a price range, color, material, etc., as well as display the results by ordering by price or availability.

Parameters:

array obj_paging ( [str table[, str cols[, str where[, str order by[, str limit[, int offset ]]]]]] )

Returns:

Array

Example:

<?php 

try 
{ 
    
$output = ''; 
    
$limit  = ( isset( $_GET["limit"] ) ) ? $_GET["limit"] : 20; 
    
$offset = ( isset( $_GET["page"] ) && $_GET["page"] > 0 ) ? $_GET["page"] : 1; 
    
    
$rs = $dbh->obj_paging( "mytable", "id,first_name,last_name", "", "id", $limit, $offset ); 
    
$result = $rs[0]; 
    
$last_page = $rs[1]; 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
     
    while( 
$row = $result->obj_fetch_object() )  
        echo 
"{$row->id}: {$row->first_name} - {$row->last_name}<br />"; 

    if ( 
$offset == 1 )  
    { 
        
$output .= "&lt;&lt;&nbsp;&nbsp;&lt;&nbsp;"; 
    }  
    else  
    { 
        
$output .= "<a href=test_paging.php?page=1>&lt;&lt;&nbsp;&nbsp;</a>"; 
        
$prev    = $offset - 1; 
        
$output .= "<a href=test_paging.php?page=$prev>&lt;&nbsp;</a>"; 
    } 

    
$output .= " [ Page $offset of $last_page ] "; 

    if ( 
$offset == $last_page )  
    { 
        
$output .= "&nbsp;&gt;&nbsp;&nbsp;&gt;&gt;"; 
    }  
    else  
    { 
        
$next    = $offset + 1; 
        
$output .=  "<a href=test_paging.php?page=$next>&nbsp;&gt;</a>"; 
        
$output .=  "<a href=test_paging.php?page=$last_page>&nbsp;&nbsp;&gt;&gt;</a>"; 
    } 

    echo 
"<p>$output</p>"; 
     
    
$result->obj_free_result(); 
     
     
    
Displays: 
     
    
1: Stephanie - Parker 
    2
: Estaban - White 
    3
: Xavier - Nichols 
    4
: Betty - Jefferies 
    5
: Stephanie - Clark 
    6
: Greg - Pipes 
    9
: Ezra - Yontz 
    11
: Marcia - Raymer 
    13
: Andrew - Jordan 
    14
: Ian - Jenkins 
    15
: Howard - Dixon 
    16
: Bonnie - Ward 
    17
: Louis - Pipes 
    18
: Orson - Schroeder 
    19
: Tiffany - Darden 
    20
: Xavier - Clark 
    21
: George - Raymer 
    22
: Cheryl - Yontz 
    24
: Percy - Cavenaugh 
    25
: David - Ingram 
     
    
<<  <  [ Page 1 of 23 ]  >  >> 
     
     
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  


try 
{ 
    
//set arguments using statement argument helper methods 
    
$output = ''; 
    
$limit  = ( isset( $_GET["limit"] ) ) ? $_GET["limit"] : 20; 
    
$offset = ( isset( $_GET["page"] ) && $_GET["page"] > 0 ) ? $_GET["page"] : 1; 
    
    
$dbh->obj_table( "mytable" );
    
$dbh->obj_cols( "id,first_name,last_name" );
    
$dbh->obj_order_by( "id" );
    
$dbh->obj_limit( $limit );
    
$dbh->obj_offset( $offset );
         
    
$rs = $dbh->obj_paging(); 
    
$result = $rs[0]; 
    
$last_page = $rs[1]; 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
     
    while( 
$row = $result->obj_fetch_object() )  
        echo 
"{$row->id}: {$row->first_name} - {$row->last_name}<br />"; 

    if ( 
$offset == 1 )  
    { 
        
$output .= "&lt;&lt;&nbsp;&nbsp;&lt;&nbsp;"; 
    }  
    else  
    { 
        
$output .= "<a href=test_paging.php?page=1>&lt;&lt;&nbsp;&nbsp;</a>"; 
        
$prev    = $offset - 1; 
        
$output .= "<a href=test_paging.php?page=$prev>&lt;&nbsp;</a>"; 
    } 

    
$output .= " [ Page $offset of $last_page ] "; 

    if ( 
$offset == $last_page )  
    { 
        
$output .= "&nbsp;&gt;&nbsp;&nbsp;&gt;&gt;"; 
    }  
    else  
    { 
        
$next    = $offset + 1; 
        
$output .=  "<a href=test_paging.php?page=$next>&nbsp;&gt;</a>"; 
        
$output .=  "<a href=test_paging.php?page=$last_page>&nbsp;&nbsp;&gt;&gt;</a>"; 
    } 

    echo 
"<p>$output</p>"; 
     
    
$result->obj_free_result(); 
     
     
    
Displays: 
     
    
1: Stephanie - Parker 
    2
: Estaban - White 
    3
: Xavier - Nichols 
    4
: Betty - Jefferies 
    5
: Stephanie - Clark 
    6
: Greg - Pipes 
    9
: Ezra - Yontz 
    11
: Marcia - Raymer 
    13
: Andrew - Jordan 
    14
: Ian - Jenkins 
    15
: Howard - Dixon 
    16
: Bonnie - Ward 
    17
: Louis - Pipes 
    18
: Orson - Schroeder 
    19
: Tiffany - Darden 
    20
: Xavier - Clark 
    21
: George - Raymer 
    22
: Cheryl - Yontz 
    24
: Percy - Cavenaugh 
    25
: David - Ingram 
     
    
<<  <  [ Page 1 of 23 ]  >  >> 
     
     
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  

?>

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

Back To Top

obj_query()

Description:

Executes general queries and is called from the objSQL class.

Parameters:

mixed obj_query ( str query )

Returns:

Result resource/object or false on failure.

Example:

<?php

try
{
    
$rs = $dbh->obj_query( "select prod_id from mytable where color='blue' and product='balloon'" );
        
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    echo 
$rs->obj_num_rows(); 
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

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

Back To Top

obj_select()

Description:

Executes a select statement with minimal SQL markup and is called from the objSQL class.

Parameters:

mixed obj_select ( [str table[, str cols[, str where[, str order by[, str sort order ]]]]] )

Returns:

Result resource/object or false on failure.

Example:

<?php  

try 
{ 
    
$rs = $dbh->obj_select( "mytable","color,size,price","product='balloons'","price" ); 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
         
    echo 
$rs->obj_num_rows(); 
     
    
$rs->obj_free_result(); 
} 
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_cols( "color,size,price" ); 
    
$dbh->obj_where( "product='balloons'" ); 
    
$dbh->obj_order_by( "price" ); 
    
$dbh->obj_sort_order( "asc" ); 
     
    
$rs = $dbh->obj_select();  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    while ( 
$obj = $rs->obj_fetch_object() )   
    {  
        
printf ( "%s %s %s \n", $obj->color,   
                                
$obj->size,
                                
$obj->price );  
    }  
      
    
$rs->obj_free_result();  
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?> 

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

Back To Top

obj_update()

Description:

Executes an update statement with minimal SQL markup and is called from the objSQL class.

Parameters:

mixed obj_update ( [str table[, array data[, str where ]]] )

Returns:

Result resource/object or false on failure.

Example:

<?php 

try 
{ 
    
$data = array( "color" => "blue", 
                   
"type"  => "leather", 
                   
"price" => 36.95 ); 
     
    
$rs = $dbh->obj_update( "products", $data, "prod_id=21" ); 
     
    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( "products" );   
    
$dbh->obj_data( array( "color" => "blue", "type" => "leather", "price" => 36.95 ) ); 
    
$dbh->obj_where( "prod_id=21" );                
     
    
$rs = $dbh->obj_update(); 
     
    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_insert, obj_paging, obj_query, obj_select

Back To Top

Batch Update Example:

<?php      

try     
{     
    
$trans = $dbh->obj_transaction();    
      
    
//array key must be a unique field in a row (ie: primary key, part #, mfg product #, etc. )  
    //array values are the data to update
    //the second argument is the column name for the unique field  
    //the third argument is the column to update  
    
$dbh->obj_batch_data( array( 12345S => 12.00,  
                                 
12345M => 12.50,  
                                 
12345L => 12.50,  
                                 
12345XL => 13.00,  
                                 
12345XXL => 13.00 ), "stock_id", "price" );    
      
    
$dbh->obj_table( "products" );    
         
    
$rs = $dbh->obj_update();     
         
    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     
}      

?>   

See also: obj_batch_data, obj_delete, obj_insert, obj_paging, obj_query, obj_select

Back To Top

obj_batch_data()

Description:

Sets the data, id and col arguments for a batch delete or update statement and is called directly from the objSQL class.


Parameters:

void obj_batch_data ( mixed data, str id[, str col ] )


Batch Update Example:

<?php      

try     
{     
    
$trans = $dbh->obj_transaction();    
      
    
//array key must be a unique field in a row (ie: primary key, part #, mfg product #, etc. )  
    //array values are the data to update
    //the second argument is the column name for the unique field  
    //the third argument is the column to update  
    
$dbh->obj_batch_data( array( 12345S => 12.00,  
                                 
12345M => 12.50,  
                                 
12345L => 12.50,  
                                 
12345XL => 13.00,  
                                 
12345XXL => 13.00 ), "stock_id", "price" );    
      
    
$dbh->obj_table( "products" );    
         
    
$rs = $dbh->obj_update();     
         
    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     
}      

?>   

See also: obj_cols, obj_data, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_table, obj_where


Batch Delete Example:

<?php     

try    
{    
    
$trans = $dbh->obj_transaction();   
     
    
//data argument can be an array or comma delimited string 
    //value(s) should be a unique field in a row (ie: primary key, part #, mfg product #, etc. )  
    
$data = array( 1234,2345,3456,4567,5678,6789 ); 
    
$data = "1234,2345,3456,4567,5678,6789"; 
     
    
//the second argument is the column name for the unique field 
    
$dbh->obj_batch_data( $data, "employee_id" );   
     
    
$dbh->obj_table( "employees" );   
            
    
$rs = $dbh->obj_delete();    
        
    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    
}     

?>   

See also: obj_cols, obj_data, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_table, obj_where

Back To Top

obj_cols()

Description:

Sets the columns argument for the obj_select, obj_row_count and obj_paging methods and is called directly from the objSQL class.


Parameters:

void obj_cols ( str cols )


Example:

<?php 

try 
{ 
    
$dbh->obj_table( "mytable" ); 
    
$dbh->obj_cols( "color,size,price" );
    
$dbh->obj_order_by( "color" );
    
    
$rs = $dbh->obj_select(); 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
         
    while ( 
$obj = $rs->obj_fetch_object() )  
    { 
        
printf ( "%s %s %s \n", $obj->color,  
                                
$obj->size,  
                                
$obj->price ); 
    } 
     
    
$rs->obj_free_result(); 
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  

?> 

See also: obj_batch_data, obj_data, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_table, obj_where

Back To Top

obj_data()

Description:

Sets the data argument for an insert or update statement and is called directly from the objSQL class.


Parameters:

void obj_data ( array table )


Example:

<?php 

try 
{ 
    
$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_batch_data, obj_cols, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_table, obj_where

Back To Top

obj_limit()

Description:

Sets the limit argument for the obj_paging method and is called directly from the objSQL class.


Parameters:

void obj_limit ( int limit )


Example:

<?php  

try  
{  
    
$dbh->obj_table( "mytable" );   
    
$dbh->obj_where( "dept='IT'" ); 
    
$dbh->obj_limit( 10 );
    
$dbh->obj_offset( $_GET["offset"] );
     
    
$rs = $dbh->obj_paging();  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    while ( 
$obj = $rs->obj_fetch_object() )    
    {   
        
printf ( "%s %s %s \n", $obj->f_name,    
                                
$obj->l_name, 
                                
$obj->salary );   
    }    
      
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_batch_data, obj_cols, obj_data, obj_offset, obj_order_by, obj_sort_order, obj_table, obj_where

Back To Top

obj_offset()

Description:

Sets the offset argument for the obj_paging method and is called directly from the objSQL class.


Parameters:

void obj_offset ( int limit )


Example:

<?php  

try  
{  
    
$dbh->obj_table( "mytable" );   
    
$dbh->obj_where( "dept='IT'" ); 
    
$dbh->obj_limit( 10 );
    
$dbh->obj_offset( $_GET["offset"] );
     
    
$rs = $dbh->obj_paging();  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    while ( 
$obj = $rs->obj_fetch_object() )    
    {   
        
printf ( "%s %s %s \n", $obj->f_name,    
                                
$obj->l_name, 
                                
$obj->salary );   
    }    
      
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_batch_data, obj_cols, obj_data, obj_limit, obj_order_by, obj_sort_order, obj_table, obj_where

Back To Top

obj_order_by()

Description:

Sets the order_by argument for the obj_select and obj_paging methods and is called directly from the objSQL class.


Parameters:

void obj_order_by ( str order_by )


Example:

<?php 

try 
{ 
    
$dbh->obj_table( "mytable" ); 
    
$dbh->obj_cols( "color,size,price" );
    
$dbh->obj_order_by( "color" );
    
    
$rs = $dbh->obj_select(); 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
         
    while ( 
$obj = $rs->obj_fetch_object() )  
    { 
        
printf ( "%s %s %s \n", $obj->color,  
                                
$obj->size,  
                                
$obj->price ); 
    } 
     
    
$rs->obj_free_result(); 
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  

?> 

See also: obj_batch_data, obj_cols, obj_data, obj_limit, obj_offset, obj_sort_order, obj_table, obj_where

Back To Top

obj_sort_order()

Description:

Sets a ascending or descending sort order argument for the obj_select method and is called directly from the objSQL class.


Parameters:

void obj_sort_order ( str asc/desc )


Example:

<?php  

try  
{  
    
$dbh->obj_table( "mytable" );  
    
$dbh->obj_cols( "color,size,price" ); 
    
$dbh->obj_order_by( "color" ); 
    
$dbh->obj_sort_order( "asc" ); 
     
    
$rs = $dbh->obj_select();  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    while ( 
$obj = $rs->obj_fetch_object() )   
    {  
        
printf ( "%s %s %s \n", $obj->color,   
                                
$obj->size,   
                                
$obj->price );  
    }  
      
    
$rs->obj_free_result();  
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?> 

See also: obj_batch_data, obj_cols, obj_data, obj_limit, obj_offset, obj_order_by, obj_table, obj_where

Back To Top

obj_table()

Description:

Sets the table argument for the obj_delete, obj_insert, obj_update, obj_select, obj_row_count and obj_paging methods and is called directly from the objSQL class.


Parameters:

void obj_table ( str table )


Example:

<?php 

try 
{ 
    
$dbh->obj_table( "mytable" ); 
    
$dbh->obj_cols( "color,size,price" );
    
$dbh->obj_order_by( "color" );
    
    
$rs = $dbh->obj_select(); 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
         
    while ( 
$obj = $rs->obj_fetch_object() )  
    { 
        
printf ( "%s %s %s \n", $obj->color,  
                                
$obj->size,  
                                
$obj->price ); 
    } 
     
    
$rs->obj_free_result(); 
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  

?> 

See also: obj_batch_data, obj_cols, obj_data, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_where

Back To Top

obj_where()

Description:

Sets the where argument for the obj_select, obj_delete, obj_update, obj_row_count and obj_paging methods and is called directly from the objSQL class.


Parameters:

void obj_where ( str where )


Example:

<?php  

try  
{  
    
$dbh->obj_table( "mytable" );   
    
$dbh->obj_where( "prod_id=1234" ); 
     
    
$rs = $dbh->obj_delete();  
      
    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_batch_data, obj_cols, obj_data, obj_limit, obj_offset, obj_order_by, obj_sort_order, obj_table

Back To Top

obj_affected_rows()

Description:

Returns the number of affected rows from an insert, update, or delete statement.

Parameters:

int obj_affected_rows ( void )

Returns:

Unsigned integer or -1 on failure.

Example:

<?php

try
{
    
$rs = $dbh->obj_update( "mytable", array( "color" => "blue" ), "id=6" );
    
    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_num_fields, obj_num_rows, obj_row_count

Back To Top

obj_fetch_assoc()

Description:

Returns a resultset as an associative array.

Parameters:

mixed obj_fetch_assoc ( void )

Returns:

Array or null if no more rows.

Example:

<?php

try
{
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    while ( 
$rs->obj_fetch_assoc() ) 
    {
        
printf ( "%s %s %s \n", $rs->obj_field( "color" ), 
                                
$rs->obj_field( "size" ), 
                                
$rs->obj_field( "price" );
    }
    
    echo 
$rs->obj_num_rows();
    
    
$rs->obj_free_result();
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_fetch_num, obj_fetch_object

Back To Top

obj_fetch_num()

Description:

Returns a resultset as an numeric array.

Parameters:

mixed obj_fetch_num ( void )

Returns:

Array or null if no more rows.

Example:

<?php

try
{
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    while ( 
$rs->obj_fetch_num() ) 
    {
        
printf ( "%s %s %s \n", $rs->obj_field( 0 ), 
                                
$rs->obj_field( 1 ), 
                                
$rs->obj_field( 2 );
    }
    
    echo 
$rs->obj_num_rows();
    
    
$rs->obj_free_result();
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_fetch_assoc, obj_fetch_object

Back To Top

obj_fetch_object()

Description:

Returns a resultset as an object.

Parameters:

mixed obj_fetch_object ( void )

Returns:

Object or null if no more rows.

Example:

<?php

try
{
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    while ( 
$obj = $rs->obj_fetch_object() ) 
    {
        
printf ( "%s %s %s \n", $obj->color, 
                                
$obj->size, 
                                
$obj->price );
    }
    
    echo 
$rs->obj_num_rows();
    
    
$rs->obj_free_result();
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_fetch_assoc, obj_fetch_num

Back To Top

obj_field()

Description:

Returns the resultset record for a numeric or associative column.

Parameters:

Associative array

mixed obj_field ( str col )

Numeric array

mixed obj_field ( int col )

Returns:

Mixed

Example:

<?php

try
{
    
//Associative array resultset
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    while ( 
$rs->obj_fetch_assoc() ) 
    {
        
printf ( "%s %s %s \n", $rs->obj_field( "color" ), 
                                
$rs->obj_field( "size" ), 
                                
$rs->obj_field( "price" );
    }
    
    echo 
$rs->obj_num_rows();
    
    
$rs->obj_free_result();
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 


try
{
    
//Numeric array resultset
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    while ( 
$rs->obj_fetch_num() ) 
    {
        
printf ( "%s %s %s \n", $rs->obj_field( 0 ), 
                                
$rs->obj_field( 1 ), 
                                
$rs->obj_field( 2 );
    }
    
    echo 
$rs->obj_num_rows();
    
    
$rs->obj_free_result();
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_fetch_assoc, obj_fetch_num, obj_fetch_object

Back To Top

obj_free_result()

Description:

Frees the statement and resultset resources from a select statement.

Parameters:

bool obj_free_result ( void )

Returns:

True on success or false on failure.

Example:

<?php 

try 
{ 
    
$rs = $dbh->obj_select( "mytable" ); 
     
    if ( 
$dbh->obj_error() ) 
        throw new 
Exception( $dbh->obj_error_message() ); 
         
    echo 
$rs->obj_num_rows(); 
     
    
$rs->obj_free_result(); 
    
} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
}  

?>

See also: obj_close_statement, obj_free_statement

Back To Top

obj_num_fields()

Description:

Returns the number of columns from a select statement.

Parameters:

int obj_num_fields ( void )

Returns:

Unsigned integer or -1 on failure.

Example:

<?php

try
{
    
$rs = $dbh->obj_select( "mytable" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    echo 
$rs->obj_num_fields();
    
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_affected_rows, obj_num_rows, obj_row_count

Back To Top

obj_num_rows()

Description:

Returns the number of rows from a select statement.

Parameters:

int obj_num_rows ( void )

Returns:

Unsigned integer or -1 on failure.

Example:

<?php

try
{
    
$rs = $dbh->obj_select( "mytable", "color,size,price", "product='balloons'", "price" );
    
    if ( 
$dbh->obj_error() )
        throw new 
Exception( $dbh->obj_error_message() );
        
    echo 
$rs->obj_num_rows();
    
}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
} 

?>

See also: obj_affected_rows, obj_num_fields, obj_row_count

Back To Top

obj_bind()

Description:

Binds values to a prepared statement as parameters.

Parameters:

void obj_bind ( mixed params )

Returns:

Void

Example:

<?php  

try  
{  
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?" );  
      
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_close_statement(); 
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_prepare_statement

Back To Top

obj_close_statement()

Description:

Closes a prepared statement and releases all result resources.

Parameters:

bool obj_close_statement ( void )

Returns:

True on success or false on failure.

Example:

<?php  

try  
{  
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?" );  
      
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_close_statement(); 
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_prepare_statement

Back To Top

obj_execute()

Description:

Executes a prepared statement.

Parameters:

mixed obj_execute ( void )

Returns:

Result resource/object or false on failure.

Example:

<?php  

try  
{  
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?" );  
      
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_close_statement(); 
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_prepare_statement

Back To Top

obj_free_statement()

Description:

Releases the previously executed prepared statement resource.

Parameters:

bool obj_free_statement ( void )

Returns:

True on success or false on failure.

Example:

<?php  

try  
{  
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?" );  
      
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
    
    
$stmt->obj_free_statement();
    
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_close_statement(); 
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_close_statement

Back To Top

obj_prepare_statement()

Description:

Prepares an SQL statement for execution.

A prepared statement is a pre-compiled SQL query that returns a resource/object. This resource can then be used to execute an SQL statement multiple times and helps reduce some overhead on the server. Prepared statements use parameterized values which are executed after the prepared statement is registered and are a proven deterent against SQL injection as the query is not exposed repeatedly.

Parameters:

mixed obj_prepare_statement ( str query[, array params ] )

Returns:

Statement resource/object or false on failure.

Examples:

<?php  

try  
{  
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?" );  
      
    
$stmt->obj_bind( 'Sales' );  
    
$stmt->obj_bind( 'Chicago' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_free_statement(); 
     
    
$stmt->obj_bind( 'HR' );  
    
$stmt->obj_bind( 'New York' );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();  
     
    
$stmt->obj_close_statement(); 
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   


//SQLSRV driver  

try  
{  
    
$params = array( &$var1, &$var2 );  
      
    
$stmt = $dbh->obj_prepare_statement( "update mytable set dept=? where location=?", $params );  
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
      
    
$stmt->obj_bind( $var1="Sales" );  
    
$stmt->obj_bind( $var2="Chicago" );    
      
    
$rs = $stmt->obj_execute();    
      
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
          
    echo 
$rs->obj_affected_rows();     
      
    
$stmt->obj_close_statement();  
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_query

Back To Top

obj_commit()

Description:

Commits the current transaction.

Parameters:

bool obj_commit ( void )

Returns:

True on success or false on failure.

Examples:

<?php

try
{
    
$trans = $dbh->obj_transaction();
    
    
$data = array( "color" => "blue",
                   
"type"  => "leather",
                   
"price" => 36.95 );
                   
    
$rs = $dbh->obj_update( "products", $data, "prod_id=21" );
    
    if ( 
$dbh->obj_error() )
        
$trans->obj_rollback();
        
    
$trans->obj_commit();    

}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
}

?>

See also: obj_rollback, obj_savepoint

Back To Top

obj_rollback()

Description:

Rolls back a transaction block.

When errors occur, obj_rollback allows you to void any transactional queries within a transaction block.

Parameters:

bool obj_rollback ( str [ name ] )

Returns:

True on success or false on failure.

Examples:

<?php 

try 
{ 
    
$trans = $dbh->obj_transaction(); 
     
    
$data = array( "color" => "blue", 
                   
"type"  => "leather", 
                   
"price" => 36.95 ); 
                    
    
$rs = $dbh->obj_update( "products", $data, "prod_id=21" ); 
     
    if ( 
$dbh->obj_error() ) 
    { 
        
$trans->obj_rollback(); 
         
        throw new 
Exception( $dbh->obj_error_message() ); 
    } 
     
    
$trans->obj_savepoint( "svp1" ); 
         
    
$data = array( "color" => "red", 
                   
"type"  => "leather", 
                   
"price" => 32.95 ); 
                    
    
$rs = $dbh->obj_update( "products", $data, "prod_id=49" ); 
     
    if ( 
$dbh->obj_error() ) 
    { 
        
$trans->obj_rollback( "svp1" ); 
        
$trans->obj_commit(); 
         
        throw new 
Exception( $dbh->obj_error_message() ); 
    } 
    else     
        
$trans->obj_commit();     

} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
} 

?>

See also: obj_commit, obj_savepoint

Back To Top

obj_savepoint()

Description:

Sets a savepoint within a transaction block.

Savepoints allow you set a point within a transaction to rollback to if a failed condition is met without affecting any work done in the transaction before the savepoint was declared.

Parameters:

bool obj_savepoint ( str name )

Returns:

True on success or false on failure.

Examples:

<?php 

try 
{ 
    
$trans = $dbh->obj_transaction(); 
     
    
$data = array( "color" => "blue", 
                   
"type"  => "leather", 
                   
"price" => 36.95 ); 
                    
    
$rs = $dbh->obj_update( "products", $data, "prod_id=21" ); 
     
    if ( 
$dbh->obj_error() ) 
    { 
        
$trans->obj_rollback(); 
         
        throw new 
Exception( $dbh->obj_error_message() ); 
    } 
     
    
$trans->obj_savepoint( "svp1" ); 
         
    
$data = array( "color" => "red", 
                   
"type"  => "leather", 
                   
"price" => 32.95 ); 
                    
    
$rs = $dbh->obj_update( "products", $data, "prod_id=49" ); 
     
    if ( 
$dbh->obj_error() ) 
    { 
        
$trans->obj_rollback( "svp1" ); 
        
$trans->obj_commit(); 
         
        throw new 
Exception( $dbh->obj_error_message() ); 
    } 
    else     
        
$trans->obj_commit();     

} 
catch ( 
Exception $e )  
{ 
    
//log error and/or redirect user to error page 
} 

?>

See also: obj_rollback, obj_savepoint

Back To Top

obj_transaction()

Description:

Initiates a transaction and disables autocommit.

Transactions are a means to ensure a database's integrity when one or more queries fail. Queries are usually grouped together in logical blocks and are committed or rolled back when certain conditions are met. In general, any statement that alters a database record such as insert, delete and update is considered a transactional statement. Statements such as create database, create table, drop database, etc. should be executed outside of a transaction block.

Parameters:

mixed obj_transaction ( void )

Returns:

Transaction instance or false on failure.

Example:

<?php

try
{
    
$trans = $dbh->obj_transaction();
    
    
$data = array( "color" => "blue",
                   
"type"  => "leather",
                   
"price" => 36.95 );
                   
    
$rs = $dbh->obj_update( "products", $data, "prod_id=21" );
    
    if ( 
$dbh->obj_error() )
        
$trans->obj_rollback();
        
    
$trans->obj_commit();    

}
catch ( 
Exception $e ) 
{
    
//log error and/or redirect user to error page
}

?>

See also: obj_commit, obj_rollback, obj_savepoint

Back To Top

obj_escape()

Description:

Escapes string data per each database type extension and is called from the objSQL class.

Parameters:

str obj_escape ( str data )

Returns:

String

Example:

<lt;?php

echo $dbh->obj_escape( "Lorem 'ipsum' dolor" );

/** 

Returns for MySQL/MariaDB

Lorem \'ipsum\' dolor

Returns for all other database extensions

Lorem ''ipsum'' dolor

*/

?>   

Back To Top

obj_info()

Description:

Returns information about objSQL, the current database server and PHP version and is called from the objSQL class.

Parameters:

array obj_info ( void )

Returns:

Array

Example:

<?php

print_r
( $dbh->obj_info() );

/** Displays:

Array ( [OBJSQL_VERSION] => 3.7.0 
        [DATABASE_NAME] => mydb 
        [DATABASE_TYPE] => mysql 
        [DATABASE_VERSION] => 50505 
        [DATABASE_CHARSET] => utf8 
        [PHP_VERSION] => 5.5.4 )

*/

?>   

Back To Top

obj_row_count()

Description:

Returns the number of rows from a select count() query and is called from the objSQL class. This helper method is dependant on the named connection instance and can be called anywhere within a query block.

Parameters:

int obj_row_count ( [str table[, str cols[, str where ]]] )

Returns:

Unsigned integer or -1 on failure.

Example:

<?php  

try  
{  
    
$num_rows = $dbh->obj_row_count( "mytable", "id", "l_name='Jones'" );  
          
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
      
    echo 
"<p>Your query returned $num_rows results</p>";  
     
}  
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_cols( "id" );  
    
$dbh->obj_where( "l_name='Jones'" );  
        
    
$num_rows = $dbh->obj_row_count();  
          
    if ( 
$dbh->obj_error() )  
        throw new 
Exception( $dbh->obj_error_message() );  
      
    echo 
"<p>Your query returned $num_rows results</p>";  
     
}  
catch ( 
Exception $e )   
{  
    
//log error and/or redirect user to error page  
}   

?>

See also: obj_affected_rows, obj_num_fields, obj_num_rows

Back To Top