Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    add up numbers from a field using php

    Hi, not sure what section to post in

    I'm trying to add up all the numbers and display the total from a field named hrs
    I tried this below but does not work, can someone show how to accomplish this?.
    its to be displayed inside my php code.
    PHP Code:
    SELECT SUM(hrs) AS hr_total FROM $db_table WHERE year $year 
    Thanks
    Sonny
    Last edited by sonny; 02-01-2012 at 05:53 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,530
    Thanks
    45
    Thanked 259 Times in 256 Posts
    The query looks fine. I assume you've tried the regular debugging of echoing the query to make sure it looks right?

    If you remove SUM(hrs) and replace with hrs, does the query give you back what you expect?

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi, can someone show what I'm doing wrong this does not work
    trying to add all numbers in the hrs field and display the total.
    PHP Code:
    $year=$_POST['year'];
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($results))
    {
    //$total=$row['total'];
    $total=$row['hrs'];
    echo 
    $total;

    Thanks
    Sonny

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    Hi, can someone show what I'm doing wrong this does not work
    trying to add all numbers in the hrs field and display the total.
    PHP Code:
    $year=$_POST['year'];
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($results))
    {
    //$total=$row['total'];
    $total=$row['hrs'];
    echo 
    $total;

    Thanks
    Sonny
    You should be using $row['SUM(hrs)'] I'm pretty sure - you can make sure by inserting a var_dump($row) just before you assign $total. Or, you could change your query to alias the sum - SUM(hrs) as total - then use $row['total'].
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    This adding up stuff is confusing the heck out of me all day, what do you mean by "Or, you
    could change your query to alias the sum - SUM(hrs)"? can you show, do I just switch hrs with as total?

    I'm just looking to display the total sum from a "hrs" field based on a year value.

    Thanks
    Sonny

    PS your signature just helped me out with a form I had.

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    This adding up stuff is confusing the heck out of me all day, what do you mean by "Or, you
    could change your query to alias the sum - SUM(hrs)"? can you show, do I just switch hrs with as total?

    I'm just looking to display the total sum from a "hrs" field based on a year value.

    Thanks
    Sonny

    PS your signature just helped me out with a form I had.
    Have you edited your posts any? Or have I just not been paying attention haha. You're already using an alias in your query. An alias is a nickname for something else in MySQL. So, your query - SELECT SUM(hrs) AS total FROM $table WHERE year='$year' - selects the SUM of hrs and calls that value total. So, you would access it using $row['total']. The problem is your PHP, not your MySQL - although it would seem you don't know much about aliases.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi
    Nope, I had no edits to my original post which has the alias in it that was my first attempt I tried earlier today

    as far as the php part goes, there's not much to that it's exactly as posted below,
    there is only a echo and that its. so you say nothing is wrong with the mysql syntax
    so I must be close I guess.

    PHP Code:
    // I'm connecting fine
    $year="2012";
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($results))
    {
    echo 
    $row['total'];


  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    Hi
    Nope, I had no edits to my original post which has the alias in it that was my first attempt I tried earlier today

    as far as the php part goes, there's not much to that it's exactly as posted below,
    there is only a echo and that its. so you say nothing is wrong with the mysql syntax
    so I must be close I guess.

    PHP Code:
    // I'm connecting fine
    $year="2012";
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($results))
    {
    echo 
    $row['total'];

    That should be correct. You're not doing any error reporting on your mysql_query though, which you really should be. Try adding a check for $result being false, or just adding 'or die(mysql_error())' to the end of the mysql_query() function.

    But, regardless, you still have a problem which I can't believe I didn't notice. Your query result is saved into $result, but you're fetching the rows from $results - make them the same .

    Just another wee note as well, you should indent your code to make it more readable. It may not seem important just now, but for large scripts it makes a huge difference.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #9
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Hi good idea I forgot to add error checking, don't whats wrong now I get this
    PHP Code:
    PHP Warning:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 
    I'm connecting and the table and columns exist, I see nothing wrong with the syntax

    PHP Code:
    <?php
    require("connect.php");
    $year="2012";
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($result)) or die(mysql_error());
    {
    echo 
    $row[total];
    }

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    Hi good idea I forgot to add error checking, don't whats wrong now I get this
    PHP Code:
    PHP Warning:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 
    I'm connecting and the table and columns exist, I see nothing wrong with the syntax

    PHP Code:
    <?php
    require("connect.php");
    $year="2012";
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'"); 
    while(
    $row mysql_fetch_assoc($result)) or die(mysql_error());
    {
    echo 
    $row[total];
    }
    You're error reporting the wrong bit.
    PHP Code:
    <?php
    require("connect.php");
    $year="2012";
    $table="test";
    $result mysql_query("SELECT SUM(hrs) AS total FROM $table WHERE year='$year'") or die(mysql_error()); 
    while(
    $row mysql_fetch_assoc($result));
    {
        echo 
    $row[total];
    }
    Also, why are you putting year and table into variables, then putting those variables into the query? Why not just put the values into the query to begin with?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #11
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Still not working, but get no errors, now

    the db has 3 entry's in that column named (hrs), numbers "2' "4" and "5"
    $row[total]; should echo "11" right?, I just get a white page.
    I even removed the year column from the query. this is the
    complete page I'm using. I'm stumped
    PHP Code:
    <?php

    require("connect.php");

    $result mysql_query("SELECT SUM(hrs) AS total FROM test") or die(mysql_error());

    while(
    $row mysql_fetch_assoc($result));
    {
        echo 
    $row[total];
    }
    ?>
    Thanks
    Sonny

  • #12
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    Still not working, but get no errors, now

    the db has 3 entry's in that column named (hrs), numbers "2' "4" and "5"
    $row[total]; should echo "11" right?, I just get a white page.
    I even removed the year column from the query. this is the
    complete page I'm using. I'm stumped
    PHP Code:
    <?php

    require("connect.php");

    $result mysql_query("SELECT SUM(hrs) AS total FROM test") or die(mysql_error());

    while(
    $row mysql_fetch_assoc($result));
    {
        echo 
    $row[total];
    }
    ?>
    Thanks
    Sonny
    Try this:
    PHP Code:
    <?php

    require("connect.php");

    $result mysql_query("SHOW COLUMNS FROM test") or die(mysql_error());

    while(
    $row mysql_fetch_assoc($result));
    {
        
    var_dump($row);
    }
    ?>
    And tell me what it spits out (probably better viewing source when checking the output).
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #13
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    I get bool(false)

    the hrs column is (int)5
    defined as 0

    Sonny

  • #14
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by sonny View Post
    I get bool(false)

    the hrs column is (int)5
    defined as 0

    Sonny
    Copy and paste the exact output.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #15
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    that's everything
    PHP Code:
    bool(false


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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