bb_count_last_query

DB Helpers.

Description

bb_count_last_query( (string) $query = '' ); 

Parameters (1)

0. $query — Optional. (string) => ''
The query.

Usage

  1. if ( !function_exists( 'bb_count_last_query' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-core.php'; 
  3.  
  4. // The query. 
  5. $query = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bb_count_last_query($query); 
  9.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-core.php  
  1. function bb_count_last_query( $query = '' ) { 
  2. global $bbdb, $bb_last_countable_query; 
  3.  
  4. if ( $query ) 
  5. $q = $query; 
  6. elseif ( $bb_last_countable_query ) 
  7. $q = $bb_last_countable_query; 
  8. else 
  9. $q = $bbdb->last_query; 
  10.  
  11. if ( false === strpos($q, 'SELECT') ) 
  12. return false; 
  13.  
  14. if ( false !== strpos($q, 'SQL_CALC_FOUND_ROWS') ) 
  15. return (int) $bbdb->get_var( "SELECT FOUND_ROWS()" ); 
  16.  
  17. $q_original = $q; 
  18.  
  19. $q = preg_replace( 
  20. array('/SELECT.*?\s+FROM/', '/LIMIT [0-9]+(\s*, \s*[0-9]+)?/', '/ORDER BY\s+.*$/', '/DESC/', '/ASC/'),  
  21. array('SELECT COUNT(*) FROM', ''),  
  22. $q 
  23. ); 
  24.  
  25. if ( preg_match( '/GROUP BY\s+(\S+)/', $q, $matches ) ) 
  26. $q = str_replace( array( 'COUNT(*)', $matches[0] ), array( "COUNT(DISTINCT $matches[1])", '' ), $q ); 
  27.  
  28. if ( !$query ) 
  29. $bb_last_countable_query = ''; 
  30.  
  31. $q = apply_filters( 'bb_count_last_query', $q, $q_original ); 
  32.  
  33. return (int) $bbdb->get_var($q);