Opens a new connection to a named database server.
mixed objSQL ( str driver, str host, str user, str pass, str database[, int port ] )
Arraymixed objSQL ( array ( str driver, str host, str user, str pass, str database[, int port ] ) )
Connection resource or object.
<?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
Closes connection to a named database server.
bool obj_close ( void )
True on success or false on failure.
<?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
Error flag for the last error.
bool obj_error ( void )
True on last error or false if no error.
<?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
String description of the last error.
mixed obj_error_message ( void )
Verbose error message or null if no error.
<?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
Executes a delete statement with minimal SQL markup and is called from the objSQL class.
mixed obj_delete ( [str table[, str where ]] )
Result resource/object or false on failure.
<?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
<?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
Executes an insert statement which requires no SQL markup and is called from the objSQL class.
See also: Inserting Multiple Rows
mixed obj_insert ( [str table[, array data ]] )
Result resource/object or false on failure.
<?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
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
}
?>
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.
array obj_paging ( [str table[, str cols[, str where[, str order by[, str limit[, int offset ]]]]]] )
Array
<?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 .= "<< < ";
}
else
{
$output .= "<a href=test_paging.php?page=1><< </a>";
$prev = $offset - 1;
$output .= "<a href=test_paging.php?page=$prev>< </a>";
}
$output .= " [ Page $offset of $last_page ] ";
if ( $offset == $last_page )
{
$output .= " > >>";
}
else
{
$next = $offset + 1;
$output .= "<a href=test_paging.php?page=$next> ></a>";
$output .= "<a href=test_paging.php?page=$last_page> >></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 .= "<< < ";
}
else
{
$output .= "<a href=test_paging.php?page=1><< </a>";
$prev = $offset - 1;
$output .= "<a href=test_paging.php?page=$prev>< </a>";
}
$output .= " [ Page $offset of $last_page ] ";
if ( $offset == $last_page )
{
$output .= " > >>";
}
else
{
$next = $offset + 1;
$output .= "<a href=test_paging.php?page=$next> ></a>";
$output .= "<a href=test_paging.php?page=$last_page> >></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
Executes general queries and is called from the objSQL class.
mixed obj_query ( str query )
Result resource/object or false on failure.
<?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
Executes a select statement with minimal SQL markup and is called from the objSQL class.
mixed obj_select ( [str table[, str cols[, str where[, str order by[, str sort order ]]]]] )
Result resource/object or false on failure.
<?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
Executes an update statement with minimal SQL markup and is called from the objSQL class.
mixed obj_update ( [str table[, array data[, str where ]]] )
Result resource/object or false on failure.
<?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
<?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
Sets the data, id and col arguments for a batch delete or update statement and is called directly from the objSQL class.
void obj_batch_data ( mixed data, str id[, str col ] )
<?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
<?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
Sets the columns argument for the obj_select, obj_row_count and obj_paging methods and is called directly from the objSQL class.
void obj_cols ( str cols )
<?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
Sets the data argument for an insert or update statement and is called directly from the objSQL class.
void obj_data ( array table )
<?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
Sets the limit argument for the obj_paging method and is called directly from the objSQL class.
void obj_limit ( int limit )
<?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
Sets the offset argument for the obj_paging method and is called directly from the objSQL class.
void obj_offset ( int limit )
<?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
Sets the order_by argument for the obj_select and obj_paging methods and is called directly from the objSQL class.
void obj_order_by ( str order_by )
<?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
Sets a ascending or descending sort order argument for the obj_select method and is called directly from the objSQL class.
void obj_sort_order ( str asc/desc )
<?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
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.
void obj_table ( str table )
<?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
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.
void obj_where ( str where )
<?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
Returns the number of affected rows from an insert, update, or delete statement.
int obj_affected_rows ( void )
Unsigned integer or -1 on failure.
<?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
Returns a resultset as an associative array.
mixed obj_fetch_assoc ( void )
Array or null if no more rows.
<?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
Returns a resultset as an numeric array.
mixed obj_fetch_num ( void )
Array or null if no more rows.
<?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
Returns a resultset as an object.
mixed obj_fetch_object ( void )
Object or null if no more rows.
<?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
Returns the resultset record for a numeric or associative column.
mixed obj_field ( str col )
Numeric arraymixed obj_field ( int col )
Mixed
<?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
Frees the statement and resultset resources from a select statement.
bool obj_free_result ( void )
True on success or false on failure.
<?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
Returns the number of columns from a select statement.
int obj_num_fields ( void )
Unsigned integer or -1 on failure.
<?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
Returns the number of rows from a select statement.
int obj_num_rows ( void )
Unsigned integer or -1 on failure.
<?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
Binds values to a prepared statement as parameters.
void obj_bind ( mixed params )
Void
<?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
Closes a prepared statement and releases all result resources.
bool obj_close_statement ( void )
True on success or false on failure.
<?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
Executes a prepared statement.
mixed obj_execute ( void )
Result resource/object or false on failure.
<?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
Releases the previously executed prepared statement resource.
bool obj_free_statement ( void )
True on success or false on failure.
<?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
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.
mixed obj_prepare_statement ( str query[, array params ] )
Statement resource/object or false on failure.
<?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
Commits the current transaction.
bool obj_commit ( void )
True on success or false on failure.
<?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
Rolls back a transaction block.
When errors occur, obj_rollback allows you to void any transactional queries within a transaction block.
bool obj_rollback ( str [ name ] )
True on success or false on failure.
<?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
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.
bool obj_savepoint ( str name )
True on success or false on failure.
<?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
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.
mixed obj_transaction ( void )
Transaction instance or false on failure.
<?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
Escapes string data per each database type extension and is called from the objSQL class.
str obj_escape ( str data )
String
<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
*/
?>
Returns information about objSQL, the current database server and PHP version and is called from the objSQL class.
array obj_info ( void )
Array
<?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 )
*/
?>
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.
int obj_row_count ( [str table[, str cols[, str where ]]] )
Unsigned integer or -1 on failure.
<?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