Click here to Skip to main content
15,306,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am fetching my data using GROUP BY but now I added pagination in my code and it require COUNT query to count the number of rows in i have also used GROUP BY in that COUNT query .. but it is not working properly.. can anyone help me out?? here is my code
$query ="SELECT COUNT(*)
         FROM import
         {$WHERE} GROUP BY mobno,telecaller";
$result = mysql_query($query);
$total_records = mysql_result($result,0);
$total_pages = ceil($total_records / $records_per_page);
//Set page number
$pageno = (isset($_GET['pageno'])) ? intval($_GET['pageno']) : 1;
$pageno = min(max($pageno, 1), $total_pages);
$LIMITSTART = ($pageno - 1) * $records_per_page;
$query = "SELECT date, mobno, city, state, type, telecaller
          FROM import
          GROUP BY mobno,telecaller
          ORDER BY date DESC
          LIMIT  $LIMITSTART, $records_per_page";
$result = mysql_query($query);
print"<div id='print'>";
echo"<center>List of Mobile Numbers for Telecaller <font color='#FF00FF'><U> $_POST[select]\r</U> </font> <center> <br/>";
echo "<table border='1' cellspacing='1' cellpadding='6'>
<tr bgcolor='#82CAFF'>
<th>Mobile No</th>
echo "<tr>";
echo "<td>". $srno1. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>".  $row['mobno'] ."</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['state'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['telecaller'] . "</td>";
echo "</tr>";

Mehdi Gholam 3-Oct-12 2:17am
"not working properly" is not a question that can be answered meaningfully.
project virus 3-Oct-12 2:34am
not working properly means.. when I use above count query without GROUP BY it shows/display all record as main query... but last some pages are blank .. which not perfect.
but when I use COUNT query using GROUP BY it display only first page not other pages..

1 solution

Change the query to:

$query ="SELECT mobno, telecaller, COUNT(*)<br />
         FROM import<br />
         {$WHERE} GROUP BY mobno,telecaller";

The reason being that the GROUP BY (and HAVING) clause can only be applied to columns selected for output.

From the MySQL online reference at[^]:

Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column names, column aliases, or column positions. Column positions are integers and begin with 1:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900