Addding numbers inside array?!

Discussion in 'PHP' started by jmansa, Mar 25, 2009.

  1. #1
    I'm trying to figure this out. I want to be able to print out the total amount of "points" for a specific user depending on rows from a db table. I have an idea that I should get all the records from the user into an array and add the numbers here?
    $result = mysql_query("SELECT * FROM ".$prefix."_numbers WHERE userid=$clubuser") or exit(mysql_error());
    				while($row = mysql_fetch_array($result)){
    					
    					Some array and output here?
    				}
    PHP:
    Please help :)
     
    jmansa, Mar 25, 2009 IP
  2. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When possible, let the database handle simple calculations and filter results for you.
    This minimizes the amount of data your database (server) needs to send to your website (server).

    You may be able to simply change your SQL query to something like this:
    SELECT SUM(points) as total FROM mytable WHERE something=somethingelse
    Code (markup):
    Such a query will return a single result row with a single "column" called "total". No array needed here.
     
    centralb, Mar 25, 2009 IP
  3. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There are many security vulnerabilities in the code fragment you posted. Before you allow access to your script or website, be sure to learn about avoiding MySQL injection vulnerabilities and other security concerns when developing PHP applications.
     
    centralb, Mar 25, 2009 IP
  4. Ilyesoft

    Ilyesoft Banned

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, I don't think you need an array, just SUM will do everything
     
    Ilyesoft, Mar 25, 2009 IP