Search code examples
phpcountcomparison

Count all values that are greater than or less than 20 from a MySQL Database


I need some code to get a count of values that are less than or greater than 20 out of my MySQL DB It should make a output of how many values there are. Any ideas?


Solution

  • Set a count initial to zero. Select the table from the mysql and loop through each variable if the variable is greater than 20 then count++. At last take the value of count which will be our expected result.

    $count=0;
    $result=mysql_query("SELECT column_name FROM table_name");
    while($row=mysql_fetch_assoc($result))
    {
    if($row['column_name']>20)
    {
    $count++;
    }
    }
    echo $count;