Re: Sum function
| From: | David Robley | Date: | Mon, 19 Feb 2001 01:59:33 +0000 |
| Subject: | Re: Sum function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-40493@lists.php.net to get a copy of this message | ||
On Mon, 19 Feb 2001 06:57, Claudia wrote:
> I am looking for code that explains how to sum a value in a database
> based on a current field retrieved from the database.
>
> Example:
>
> query = "select course_name, course_build, milestone, testcase,
> time_tested, issue_status, comment,
> bug_number, tested_by, date_tested from testissues where
> test_issue_id = $viewid";
> $result = mysql_query( $query );
>
> 1 - retrieve needed fields from database based on the id the user links
> from ($viewid)
> 2 - assign the course_name value from the query statement to a variable
> to be used on html page
>
> Assign: $course_name = mysql_result( $result, 0, "course_name" );
>
> Use variable: print " <td><font color =
> #006666><b>$course_name</b></font></td>\n";
>
>
> Now I would like to sum ALL the time_tested values in my database WHERE
> the course_name =
> $course_name (the variable retrieved for the record)
>
> query = "select sum(time_tested) where course_name = $course_name";
> $result = mysql_query( $query );
> $total_time = mysql_result( $result, 0, "sum(time_tested)");
>
> First issue -- php does not seem to recognize the value of $course_name
Do you mean here:
query = "select sum(time_tested) where course_name = $course_name";
because in that query $course_name should be enclosed in single quotes if
it is a char type field.
> Second issue -- will the $total_time value be calculated correctly
> based on the above syntax?
What type of field is time_tested?
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA