Fido Fido - 7 months ago 28
SQL Question

Mysql count column issue

I can't figure-it out how to count all these dancers columns and echo with total of all dancers

id | dancer1 | dancer2 | dancer3 | dancer4, and so on..
---------------------------------------
1 alex michael dalm name
2 clare rose test


I have this for the start but is not working:

$counter = mysql_query("SELECT COUNT(*) AS id FROM table");

$num = mysql_fetch_array($counter);
$dancers = $num["id"];

echo "Total dancers: $dancers";


Any help is appreciated. Thanks!

Answer

Try this:

$counter = mysql_query("SELECT * FROM table");
$dancers = 0;
while($rows = mysql_fetch_array($counter)){
    for($i = 1; $i <= 24; $i++){
        $dan_id = 'dancer'.$i;
        if($rows[$dan_id] != "" || $rows[$dan_id] != null )
            $dancers++;
    }   
}
echo "Total dancers:". $dancers;

Note: Never design your database table like this.

Comments