COUNT(*), PHP and MySQL

wjh2303

New Member
Messages
139
Reaction score
0
Points
0
i wish to count the number of rows with a given content, group them by one of the columns and print the results:

code:
$countdet=mysql_query("SELECT size, COUNT(*) FROM Details WHERE mapLoc = '$rowloc[id]' GROUP BY size") or die(mysql_error());

while($rowcd=mysql_fetch_array($countdet)){
switch($rowcd[size]){
case "GP":
echo "<img src=\"GP.png\"> $rowcd['COUNT(*)'] Gigapixel images<br>";
break;
case "L":
echo "<img src=\"L.png\"> $rowcd['COUNT(*)'] large images<br>";
break;
case "M":
echo "<img src=\"M.png\"> $rowcd['COUNT(*)'] medium images<br>";
break;
case "S":
echo "<img src=\"S.png\"> $rowcd['COUNT(*)'] small images";
break;
}
}


as above, i get the error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

and if i change $rowcd['COUNT(*)'] to $rowcd[COUNT(*)] i get the error: unexpected ( was expecting ].

any ideas? i just want it to return the value of the count... the mysql query works fine when i put it in, and the line number on the errors point to this bit of the code

TIA

edit: the example on this (http://www.tizag.com/mysqlTutorial/mysqlcount.php) is pretty much what i want to achieve, but am failing to do
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
252
Points
63
Array indexes cannot contain whitespace or asterisks. You can use mysql_fetch_array, which creates an array with both associative and numeric indexes, or you can alias the count() result:

Code:
SELECT size, COUNT(*) AS count FROM ....

Now you can use $rowcd['count'] or "{$rowcd[count]}" I use {} around all PHP variables, so PHP automatically knows where the variable starts and where it ends :)
 
Top