Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP MySQL
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 table.so 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
          {$WHERE}
          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>Sr.No</th>
<th>Date</th>
<th>Mobile No</th>
<th>City</th>
<th>State</th>
<th>Type</th>
<th>CIExe.</th>
</tr>";
//if(isset($_POST['submit']))
//{
$srno1=0;
while($row=mysql_fetch_array($result))
{
    $srno1=$srno1+1;
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>";
}
echo"</table>";

THANKS IN ADVANCE..
Posted 2-Oct-12 21:05pm
Comments
Mehdi Gholam at 3-Oct-12 2:17am
   
"not working properly" is not a question that can be answered meaningfully.
project virus at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Change the query to:

$query ="SELECT mobno, telecaller, COUNT(*)
FROM import
{$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 http://dev.mysql.com/doc/refman/5.0/en/select.html[^]:

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:
  Permalink  

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

  Print Answers RSS
0 Suvendu Shekhar Giri 215
1 Sergey Alexandrovich Kryukov 215
2 Andy Lanng 210
3 PIEBALDconsult 190
4 Abhinav S 184
0 Sergey Alexandrovich Kryukov 6,360
1 OriginalGriff 5,878
2 Peter Leow 2,514
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web02 | 2.8.150414.1 | Last Updated 18 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100