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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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