Generating Categories & Counting Rows


I have a table called horses, with current data as follows: … _table.jpg

I would like to have a list of the breeds for each gender, showing how many results there are, i.e,

Abaco Barb (3)
Belgian Draft Horse (1)

mysql_connect(“", "”, “") or die(mysql_error());
”) or die(mysql_error());

    $data = mysql_query("SELECT gender, COUNT(*) SELECT * FROM `horses` WHERE gender = 'mare' AND allowed='OK' GROUP BY breed;")  
    or die(mysql_error());  


The end result is a blank page. Could someone advise me where I am going wrong? I plan on turning the breed names into links, is there a better way of doing this? I plan on producing a second table with breed name, gender and url.

Many thanks.

Your query is invalid.

COUNT is an aggregate function and unless used with a “Group” you can only use count in the select statement and generally you don’t count(*) but you count a specific field (typically the ID or INDEXED field).

without getting into sub-queries, you can only have ONE “select” per query.

You can do a
SELECT * FROM horses WHERE gender = ‘mare’ AND allowed = ‘OK’ ORDER BY breed

Sponsor our Newsletter | Privacy Policy | Terms of Service