Jump to content

Counting Records in PHP


stevesimo

Recommended Posts

Hi, I have a table which contains categories.  These categories are output so the user can select which one they want to view.  What I want to do is count how many records there are for each category and output this next to the category name.  For example cars(5), bikes(3) etc.

 

Whilst I have my code working which outputs the category, I am not sure how to use the SQL count operator.  Can anyone offer any advice on the best way to achieve this.

 

Here is my code:

 


$query  = "Select * From Categories Order By categoryTitle";
$result = mysql_query($query);
echo '<table>';
  echo '<tr>';
    echo '<td width="150">';
    while ($row = mysql_fetch_array($result)){//loop through dropdown row by row
      $categoryID     = $row['categoryID']; 
      $categoryTitle  = $row['categoryTitle']; 
      $count = $count + 1;
      echo "<a href='search.php?area=".$id."&category=".$categoryID."'>".$categoryTitle."</a><br>";
}//end of loop

[code]

thanks, Steve

[/code]

Link to comment
https://forums.phpfreaks.com/topic/97043-counting-records-in-php/
Share on other sites

you can count the number of result lines of a MySQL Query with the folowing Syntax:

 

"SELECT COUNT(*) FROM my_table WHERE Category=cat_to_count"

 

(you can also replace the asteriks (*) with only one column name (like ID) but I'm not sure if it gets faster because of this.

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.