Multiple count in php


#1

Hi,

I’ve got to use multiple counts in php. Now I do it like this:

[php]$res5 = mysql_query(“SELECT count(cars) FROM mytable WHERE car=‘bmw’ and cars!=’’”) or die(mysql_error());
list($bmws)=mysql_fetch_row($res5);
$res6 = mysql_query(“SELECT count(boats) FROM mytable WHERE boat=‘bavaria’ and boats!=’’”) or die(mysql_error());
list($boats)=mysql_fetch_row($res6);
$res7 = mysql_query(“SELECT count(motos) FROM mytable WHERE moto=‘suzuki’ and moto!=’’”) or die(mysql_error());
list($motos)=mysql_fetch_row($res7);
[/php]
They fields are all in the same table. Is there a way to have on select instead of three?
I would appreciate your help.
Thanks in advance


#2

Yes. Union, or a self join works. But, stop using old mysql_ functions and hop on the PDO bandwagon.
The data model looks like a wreck though.


#3

The first thing you need to do is fix your database design. You need to learn about database normalization.