SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 54
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is wrong with this small piece of code?!

    What is wrong with this small piece of code?!

    PHP Code:
    <?php 
    $sql 
    "SELECT variable1, variable2, variable3, variable4, variable5, (variable1 + variable2 + variable3 + variable4 + variable5) AS total FROM table"
    $result mysql_query($sql);  $total 0

    while (
    $row mysql_fetch_array($result)) {   
    $total += $row['variable1 + variable2 + variable3 + variable4 + variable5']; 
    }  

    echo 
    "Total: " $total;  

    or die(
    "INSERT Error: ".mysql_error());
    ;
    ?>

  2. #2
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use this.

    PHP Code:
    $total += $row['total']; 
    Barbara

  3. #3
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using MySQL's SUM function, I think, would be a much better way of doing this.

    But yes, you aliased your sum as the name total, so you use total.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  4. #4
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I keep getting a blank page when I do that

  5. #5
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My new code is the following, but I get a blank page

    PHP Code:
     <?php $sql "SELECT variable1, variable2, variable3, variable4, variable5, (variable1 + variable2 + variable3 + variable4 + variable5) AS total FROM table"$result mysql_query($sql);  $total 0;  while ($row mysql_fetch_array($result)) {    $total += $row['total']; }    echo "Total: " $total;    or die("INSERT Error: ".mysql_error()); ; ?>

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You want to put the or die part after the query itself, not in the code afterwords. Also, perhaps you still need to use tha tSUM function?

    Try the "or die" and see what MySQL might complain about.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  7. #7
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should something that time, but it didn't add the numbers together.

  8. #8
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you put the or die statement after your mysql_query() to see if MySQL is giving an error? That's what I was trying to say in my last post.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  9. #9
    SitePoint Enthusiast Chousho's Avatar
    Join Date
    Jun 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I did, but it didn't die. It says Total: 0 but, it should be 24 as it should have added the numbers.

  11. #11
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I may need to select from a user variable two so it knows where to look to caculate it. That may be the problem, but I am not sure how to add that in there.

  12. #12
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're sure that the table has rows?

    And again, why aren't you using SUM? You've consistently refused to answer that...SUM is there for a purpose. Someone above even linked to the documentation.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  13. #13
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand the Sum concept.. I'm not sure the syntax for it either.

    here is my updated code, that doesn't work still:

    PHP Code:
    <?php 
    mysql_select_db
    (time_com$connection);

    $sql "SELECT monreghours0, monreghours1, monreghours2, monreghours3, monreghours4 AS total FROM table WHERE username='$_SESSION[user_name]'"); 
    $result mysql_query($sql);  

    $total 0
    while (
    $row mysql_fetch_array($result)) {   
    $total += $row['total']; 
    }  

    echo 
    "Total: " $total;  

    ?>

  14. #14
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now you're just aliasing monreghours4 to total, which I'm assuming you don't want to do?

    Please explain exactly what you are trying to add together.

    As for the syntax of SUM, Chousho linked to the MySQL documentation above.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  15. #15
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I have a form that a user fills out.. when they fill out that form data gets placed into different rows monreghours0, monreghours1 etc are column names.. I need to add these numbers together dependant on the user that row corresponds to.

  16. #16
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, I am not sure what to put in places in says (sum), in regards to the syntax for SUM

  17. #17
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK how about

    Code:
    SELECT monreghours0 + monreghours1 + monreghours2 + monreghours3 + monreghours4 AS total FROM table WHERE id = ...
    You should use the user ID in the where clause instead of username, probably.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  18. #18
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    still a blank screen.

  19. #19
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK listen...where you have

    PHP Code:
    $result mysql_query($sql); 
    As I said before, you need to do:

    PHP Code:
    $result mysql_query($sql) or die(mysql_error()); 
    Then it should tell you if you have an error.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  20. #20
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added that line in, it still a blank page it tells me nothing.

  21. #21
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Code:
    $sql = 
    	"SELECT 
    		sum(monreghours0 +
    		monreghours1 +
    		monreghours2 + 
    		monreghours3 + 
    		monreghours4) AS total 
    	FROM 
    		table 
    	WHERE 
    		username='". $_SESSION['user_name'] ."'
    	");
    try that
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  22. #22
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is updated code:

    <?php
    mysql_select_db(time_com, $connection);

    $sql = "SELECT monreghours0, monreghours1, monreghours2, monreghours3, monreghours4 AS total FROM table WHERE id ='$_SESSION[user_name]'");
    $result = mysql_query($sql) or die(mysql_error());

    $total = 0;
    while ($row = mysql_fetch_array($result)) {
    $total += $row['total'];
    }

    echo "Total: " . $total;

    ?>

  23. #23
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still getting a blank page with that spike, and it's providing me no error message.

  24. #24
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If that's all of your code, you're not connecting to the db, and using an undefined constant time_com.

    If that's not all of your code, place this on top of your script to turn on the error reporting:
    Code php:
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    Saul

  25. #25
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've defined the $connections variable at the top of the document.


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
  •