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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.