Re: array count()
| From: | David Robley | Date: | Tue, 11 Jul 2000 06:35:33 +0000 |
| Subject: | Re: array count() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-5863@lists.php.net to get a copy of this message | ||
On 10 Jul, Brad Sacks wrote:
> I thank you for your answer. What I really want to do is count how many
> records are in the db table that are in a catagory. I have 7 catagories and
> want to return a count of each without making 7 queries.
>
> David Robley wrote:
>
>> On 10 Jul, Brad Sacks wrote:
>> > I have an record set from a MySql database;
>> > $record = mysql_fetch_array($result);
>> > I want to be able to count the number of elements inside that have the
>> > same key. Like $record["blah"];
>> > I want to know how many record blahs there are in the record set. I hope
>> > someone out there can help. I used $num = count($record["advoc"]); but I
>> > new it was wrong and it returned 1. when there are 12 records. I've been
>> > pouring over the manual for an hour.
>> >
>>
>> I'm not quite sure that I understand what it is you are asking, but I'll
>> answer anyway :-)
>>
>> First up, $record only contains one row from your table. Perhaps you
>> want to use mysql_num_rows to find out how many rows were returned?
>> Alternatively, if you want to find out how many rows in a table match an
>> expression or value in a particular field or fields, you can use the
>> MySQL COUNT() function.
>>
>> eg SELECT COUNT(*) AS howmany FROM contacts WHERE name LIKE '%SMITH%'
>>
>> will return one row containing one field howmany, showing how many names
>> like SMITH are in your contacts table.
Aha - you want MySQLs COUNT() in conjunction with GROUP BY column to do
that. Roughly, SELECT COUNT(*) AS howmany GROUP BY category
which should return you as many rows as there are categories.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA