Combination Arithmetic & Assignment Operators

Discussion in 'PHP' started by tyankee, Jan 28, 2014.

  1. #1
    i'm trying to add 25 to the result of a mysql sum query and it's telling me:

    Unsupported operand types in /home/content/s/a/l/xxxxxxx/html/people/index2.php on line 92


    here is the code.. i'm a newbie with PHP but this doesn't make sense..

    $resultm = mysql_query("SELECT *,SUM(point.point_value) AS month_total FROM point_tracking LEFT JOIN point ON point_tracking.point_id=point.point_id WHERE point_tracking.member_id='".$_SESSION['id']."' AND entry_date LIKE '".date('Y-m')."%'");
    $mpoints=mysql_fetch_array($resultm);
    $mpoints = $mpoints + 25;
    $resulty = mysql_query("SELECT *,SUM(point.point_value) AS year_total FROM point_tracking LEFT JOIN point ON point_tracking.point_id=point.point_id WHERE point_tracking.member_id='".$_SESSION['id']."' AND entry_date LIKE '".date('Y')."%'");
    $ypoints=mysql_fetch_array($resulty);
    $ypoints=$ypoints + 25;

    the line with the error is "$mpoints = $mpoints + 25;"
     
    tyankee, Jan 28, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're trying to add 25 to an array...
     
    PoPSiCLe, Jan 28, 2014 IP
  3. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #3
    Yes i figure that - just don't know how to add 25 to the result of that query..
     
    tyankee, Jan 28, 2014 IP
  4. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #4
    If you just want to add 25 to the item IN the array, change it to this:
    $ypoints['month_total'] += 25;
    PHP:
    Another option would be to do it right in the SQL query...
    $resulty = mysql_query("SELECT *,SUM(point.point_value) + 25 AS year_total FROM point_tracking LEFT JOIN point ON point_tracking.point_id=point.point_id WHERE point_tracking.member_id='".$_SESSION['id']."' AND entry_date LIKE '".date('Y')."%'");
    PHP:
    Side note - you really would be better off using mysqli extensions since the ones you are using are deprecated and will be removed from PHP at some point.

    http://www.php.net/manual/en/class.mysqli.php
     
    digitalpoint, Jan 28, 2014 IP
  5. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #5

    thanks.
     
    tyankee, Jan 28, 2014 IP
  6. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #6

    the second option
    $resulty = mysql_query("SELECT *,SUM(point.point_value) + 25 AS year_total FROM point_tracking LEFT JOIN point ON point_tracking.point_id=point.point_id WHERE point_tracking.member_id='".$_SESSION['id']."' AND entry_date LIKE '".date('Y')."%'");
    
    Code (markup):

    seems to add 25 to each record instead of the total - so for example if there were 10 records, the total ends up being 250 too high..
     
    tyankee, Jan 29, 2014 IP
  7. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #7
    It shouldn't add 25 for each record unless the + 25 was inside the parentheses like so:
    SUM(point.point_value + 25)
    Code (sql):
    It should be outside, like this:
    SUM(point.point_value) + 25
    Code (sql):
     
    digitalpoint, Jan 29, 2014 IP
  8. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #8

    ahhhh - got it... thanks again..
     
    tyankee, Jan 29, 2014 IP