Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How to count data in a MySQL table using PHP?

Given the "Employees" table below:

---------------------------------------------------------------

| Name | Hours Worked |

---------------------------------------------------------------

| John | 5 |

| Bob | 3 |

| Jack | 2 |

| Jill | 7 |

| Jill | 1 |

---------------------------------------------------------------

How does one go about writing PHP code to add up all of the hours in the table?

On top of that, how does one go about adding up all the hours of a particular person (example: Jill).

3 Answers

Relevance
  • niknah
    Lv 4
    1 decade ago
    Favorite Answer

    All the hours for everyone...

    $r = mysql_query("select sum('Hours Worked') from Employees")

    Totals of a particular person...

    $r = mysql_query("select Name,sum('Hours Worked') from Employees group by Name")

    to print it out...

    while($row = mysql_fetch_array($r)) {

    echo $row[0]."=".$row[1];

    }

  • 1 decade ago

    to add to that, if you just want Jill's hours, use

    select sum('Hours Worked') from Employees

    where Name='Jill'

    as described, better to do the summing in the MySQL side and just read the answer in PHP -- otherwise you waste a lot of bandwidth and CPU pulling all that data out of the database to then manipulate it with PHP

  • ?
    Lv 4
    5 years ago

    replace this code after mysql_select_db("mydatabase") or die(); $question = mysql_query(" opt for remark FROM remark") or die(mysql_error()); if (mysql_num_rows($question) > 0 ) { on a similar time as ($row = mysql_fetch_array($question)) { extract ($row); return $call; } } ?> rejoice with!!

Still have questions? Get your answers by asking now.