Not counting rows correctly when using GROUP BY

enboig - August 25, 2008 - 08:35
Project:Ajax Table
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

I have discovered it doesn't get the number of rows correctly when the query contains "GROUP BY" and "SUM()"; so I had to change some code:

<?php
    $count_inner
= '*';
    if (
$table['count_query'] != '') {
       
$count_inner = $table['count_query'];
    }
 
$count_query = preg_replace('/SELECT (.*) FROM/','SELECT COUNT(' . $count_inner . ') FROM',$table['query']);
 
$result = db_query($count_query,$search_values);
 
$row = db_fetch_array($result);
 
$count = $row['COUNT(' . $count_inner . ')'];
 
$elements['rows'] = $count;
?>

replaced by
<?
$res_temp=db_query($table["query"]);
$count=db_num_rows($res_temp);
$elements['rows']=$count;
unset($res_temp);
?>
It may add some overhead to the database server, but it has less PHP code and works.

#1

enboig - August 25, 2008 - 08:36

sorry, my code wasn't included in php tags correctly:

<?php
  $res_temp
=db_query($table["query"]);
 
$count=db_num_rows($res_temp);
 
$elements['rows']=$count;
  unset(
$res_temp);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.