Jump to content

Count Query


neha_jaltare

Recommended Posts

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..

Link to comment
https://forums.phpfreaks.com/topic/269029-count-query/
Share on other sites

From what you have posted, the variable $WHERE is never defined. There is a tutorial on PHP Freaks itself about pagination http://www.phpfreaks.com/tutorial/basic-pagination I've used this before and it works a charm.

 

A small change I would do is add "or die(mysql_error());" on the end of your queries. Like below...

$result = mysql_query($query) or die(mysql_error());

This would mean if an error occurred with the query, it'll show you what the error is.

 

I'll also say now... "but it is not working properly" does not tell us what the problem is. You must specify what happens and what outputs (if any) and what you want it to do, this will give us a leg to stand on whilst helping you fix your issues.

Link to comment
https://forums.phpfreaks.com/topic/269029-count-query/#findComment-1382435
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.