Jump to content

Count 'checked'


Bickey

Recommended Posts

How to write the code to get the count of the word "CHECKED" from a row?

 

I tried this and it's not working. Please help.

$query = mysql_query("SELECT field1, field2, field3, COUNT(CHECKED) as counter FROM db1 WHERE row_name = 'science'");
while($line = mysql_fetch_array($query))
{
echo $query['counter'];
}

Link to comment
https://forums.phpfreaks.com/topic/225982-count-checked/
Share on other sites

ok, looking at the table below, can someone tell me how to get the count of "MAX" in row 1?

 

 

id        field1        field2        field3        field4

1        MAX          MIN          MAX          MAX

2        MIN          MIN          MIN          MAX

Link to comment
https://forums.phpfreaks.com/topic/225982-count-checked/#findComment-1166641
Share on other sites

<?php

$result = mysql_query("SELECT * FROM `table` WHERE `field1` = 'MAX';");
$count = mysql_num_rows($result);
mysql_free_result($result);

?>

 

There's no need to run a query to return back a large result set just to count the records. That is a waste of server resources. That is what COUNT() in queries is for.

Link to comment
https://forums.phpfreaks.com/topic/225982-count-checked/#findComment-1166700
Share on other sites

I think what the OP wants is to count the number of times 'MAX' occurs in a particular record.

 

You're right. He did say

How to write the code to get the count of the word "CHECKED" from a row?

I totally missed that.

 

That is actually very simple:

SELECT (  IF(field1='CHECKED', 1, 0)
        + IF(field2='CHECKED', 1, 0)
        + IF(field3='CHECKED', 1, 0)) as counter
FROM db1
WHERE row_name = 'science'

Link to comment
https://forums.phpfreaks.com/topic/225982-count-checked/#findComment-1166718
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.