SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    showing an average score

    Hi there

    I've got a mysql table where scores out of 10 can be entered for a particular person.

    In an SSH environment I can get that with the following SQL statement.

    select AVG(score) from scores where name = 'Dave';

    However I'm not sure how I can get that to display on a web page using php syntax.

    Grateful if anyone can help.

    Thanks


    Juan.

  2. #2
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by juanc
    Hi there

    I've got a mysql table where scores out of 10 can be entered for a particular person.

    In an SSH environment I can get that with the following SQL statement.

    select AVG(score) from scores where name = 'Dave';

    However I'm not sure how I can get that to display on a web page using php syntax.

    Grateful if anyone can help.

    Thanks


    Juan.
    You could get the number of entries for Dave, add up the values and divide by the number of entries.

  3. #3
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    //connecting to a server, selectting a database
    $res mysql_query("SELECT AVG(score) AS averageScore FROM scores 
                                WHERE name = 'Dave'"
    ) or die(mysql_error());
    $rec mysql_fetch_array($res);
    print(
    "The average score for Dave is " $rec["averageScore"]); 

  4. #4
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for this

    I know that's an option but really the same problem applies......

    ie. to add up the total for Dave I would use the SUM() function in SQL but then how (in terms of php) would I then feed that into a variable so as to divide it by the number of rows?

    ... I know that to get the number of rows it would be
    PHP Code:
    $num_rows mysql_num_rows($mysql_result); 
    I just need something similar that can give me the sum of the rows or the average.

  5. #5
    SitePoint Zealot
    Join Date
    May 2004
    Location
    London
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks Mike Borozdin...........I posted a reply to geniusgoalie before seeing your reply. Thanks to all and sorry for any confusion.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •