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.
Results 1 to 7 of 7

Thread: SUM() in php

  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    SUM() in php

    Hello there, i am trying to add the total amount of sales together here is what i have tried

    PHP Code:
    <?php
    $query 
    'SELECT SUM(ammount) AS SalesTotal FROM Orders WHERE user_id=' .$user->data['user_id'];

    $result mysql_query('$query');

    echo 
    $result;
    ?>
    and include it in page, but nothing is being echoed at all.

    What did i do wrong?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Cmarenburg View Post
    Hello there, i am trying to add the total amount of sales together here is what i have tried

    PHP Code:
    <?php
    $query 
    'SELECT SUM(ammount) AS SalesTotal FROM Orders WHERE user_id=' .$user->data['user_id'];

    $result mysql_query('$query');

    echo 
    $result;
    ?>
    and include it in page, but nothing is being echoed at all.

    What did i do wrong?

    Thanks!
    try this:

    PHP Code:
    $row=mysql_fetch_assoc($query);
    $sales=$row['SalesTotal']; 
    and echo it as $sales
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dan13071992 View Post
    try this:

    PHP Code:
    $row=mysql_fetch_assoc($query);
    $sales=$row['SalesTotal']; 
    and echo it as $sales
    Hey there, when i do that i am left with an error:

    [phpBB Debug] PHP Warning: in file /var/www/includes/sales-total.php on line 4: mysql_fetch_assoc() expects parameter 1 to be resource, string given

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    None of this will work. $query is not parsed within the mysql_query, it is a literal string so the query will fail.
    PHP Code:
    $qry mysql_query($query) or die(mysql_error());
    $sales mysql_result($qry00); 

  • Users who have thanked Fou-Lu for this post:

    Cmarenburg (01-28-2012)

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    None of this will work. $query is not parsed within the mysql_query, it is a literal string so the query will fail.
    PHP Code:
    $qry mysql_query($query) or die(mysql_error());
    $sales mysql_result($qry00); 
    Thank You soo much!

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Fou-Lu View Post
    None of this will work. $query is not parsed within the mysql_query, it is a literal string so the query will fail.
    PHP Code:
    $qry mysql_query($query) or die(mysql_error());
    $sales mysql_result($qry00); 

    sorry i forgot that it hadnt already gone through the mysql_query function, my mistake.

    FL thanks for your inbox the other day, i got it working in the end.

    back to the topic, im a bit confused as to what this function means, and how it works:

    PHP Code:
    $sales mysql_result($qry00); 
    so just to help Cmarenburg and myself understand it better could you give us a bit of insight into how the mysql_result and the 0, 0 actually work/what they mean.

    thanks.

    Dan
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Mysql_result is used to pull single entries from the dataset. I wouldn't use it for anything more than say a count or sum or another aggregate, since it is slower than pulling from mysql_fetch, but saves you the memory of a variable assignment. 0, 0 is simply the row and the field, since PHP is 0 based its the first row and first column.


  •  

    Posting Permissions

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