Results 1 to 6 of 6

Thread: adding database information using php

  1. #1
    Join Date
    Aug 2005
    Posts
    518
    Rep Power
    0

    Default adding database information using php

    ok... I am not a programmer... ( i am self taught.. i working with Php) i have data , numbers to be exact in a mysql database

    i can pull the data out, but what i want to know is how can i get each data to be added to the next and then displayed as a sum

    extract:

    //query database
    $gas_b = "Select number from $dbase_table3";
    $gas_b_q = mysql_query($gas_b);

    //pull data
    $inc=0;
    while ($row = mysql_fetch_array($gas_b_q)) {

    $bought = $row["amount_bought"];
    }

    $result[$inc]=$bought;
    $inc++;


    and to display it as a list

    extract:

    for ($k = 0; $k <= count($result); $k++) {
    echo $result[$k];}

    this what i have.. (what i use to pull out data and display as list) anyway.. i want to take number out and add them together and display only the sum

    how would i change this script to instead of listing i could just add them together

    and then just print the sum..

    some Help
    Why fight Information Technology when you can outsource IT

  2. #2
    Join Date
    Jan 2004
    Posts
    2,011
    Rep Power
    0

    Default

    $sum = 0;
    for ($k = 0; $k <= count($result); $k++) {
    $sum = $result[$k] + $sum;
    }

    echo $sum;

  3. #3
    Join Date
    Jul 2004
    Posts
    264
    Rep Power
    0

    Default

    well.. there are two ways you can approach this... the first is by making the database do the calculation for you via a select statement then just print the output:

    PHP Code:
    $res mysql_query("SELECT SUM(amount_bought) AS total FROM what_ever_table");
    $row mysql_fetch_array($res);
    echo 
    "SUM is $row[total]"
    OR
    the second method is to use PHP code to carry out the calculation:

    PHP Code:
    $total 0;
    foreach(
    $result as $val){
         
    $total += $val;
    }
    echo 
    "SUM is $total"
    But i hope that you know that the code you have up there has some bugs that need to be fixed.
    1. The $inc variable is supposed to be in the while loop
    2. The sql statement you have there will cause PHP to generate errors because the field(number) specified in the SQL statement will not be present in the associative array - $row.
    Last edited by aonekilla; Jul 14, 2006 at 10:49 PM.

  4. #4
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    Code:
    <?php 
    
    print( array_sum($a) ); 
    
    ?>

  5. #5
    Join Date
    Aug 2005
    Posts
    518
    Rep Power
    0

    Default

    Quote Originally Posted by aonekilla
    well.. there are two ways you can approach this... the first is by making the database do the calculation for you via a select statement then just print the output:

    PHP Code:
    $res mysql_query("SELECT SUM(amount_bought) AS total FROM what_ever_table");
    $row mysql_fetch_array($res);
    echo 
    "SUM is $row[total]"
    OR
    the second method is to use PHP code to carry out the calculation:

    PHP Code:
    $total 0;
    foreach(
    $result as $val){
         
    $total += $val;
    }
    echo 
    "SUM is $total"
    But i hope that you know that the code you have up there has some bugs that need to be fixed.
    1. The $inc variable is supposed to be in the while loop
    2. The sql statement you have there will cause PHP to generate errors because the field(number) specified in the SQL statement will not be present in the associative array - $row.

    Thanks... the filed number .. was a mistake .. it was actually amount_bought

    but anyway. . that i go it to work... using this
    as u stated

    extract

    $gas_b = "Select sum(amount_bought)AS Total from $dbase_table3";
    $gas_b_q = mysql_query($gas_b) or die(mysql_error());
    $result = mysql_fetch_array($gas_b_q);

    echo $result['Total'];

    Thanks.. but question.. is there a difference in having your database do the calucalation as to doing using php
    Why fight Information Technology when you can outsource IT

  6. #6
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    Quote Originally Posted by Overkill
    Thanks.. but question.. is there a difference in having your database do the calucalation as to doing using php

    the database is always much much much faster because it is better at handling the data

Posting Permissions

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