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

Classes

  • cubrid_connection
  • cubrid_prepare
  • cubrid_resultset
  • cubrid_statement
  • cubrid_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: Row Count

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

The SQLite3 driver does not have a native PHP num rows method and uses a simulated num rows query, but it is very inefficient and can time out even on small recordsets. The SQLite3 PDO driver returns 0 and the SQLSRV PDO driver returns -1 from the PHP rowCount() method. The obj_row_count() helper method is recommended for all situations requiring an accurate and efficient num rows count.

  • The $table name argument is required.
  • The optional $cols argument specifies which column(s) to count and is entered as a comma delimited string. Entering an empty value is equivilent to an asterisk (*).
  • The optional $where argument allows you to limit which rows are selected and only requires the column and its value. You can use any of the normal operators used in SQL statements:
    • "color IN ('blue','red')"
    • "color='red' AND material='leather'"
    • "brand LIKE 's%'"
    • "price BETWEEN 200 AND 400"

Returns: Unsigned integer or -1 if undetermined or failure

<?php

//usage: $num_rows = $dbh->obj_row_count( $table, $cols, $where )

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
} 

?>

objSQL 3.2.0 API documentation generated by ApiGen 2.8.0

Documentation licensed under a Creative Commons Attribution 3.0 Unported License.