SIGNED tinyint goes negative..

Discussion in 'PHP' started by x0x, Oct 5, 2008.

  1. #1
    mysql_query("UPDATE stuff SET lol=lol-5 WHERE id>0 and lol> 0;");


    field lol is tinyint and unsigned. When lol is 1 and it takes off 5, it goes explodes and jumps to 4 trillion. Why, how, why?
     
    x0x, Oct 5, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    make it unsigned so that it is always positive
     
    ads2help, Oct 5, 2008 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Sorry, I meant to say UNSIGNED. It is already unsigned. Also the problem is not that it goes negative, the problem is that it jumps to 4 TRILLION, it's the maximum amount tinyint can old I guess. Any cure?
     
    x0x, Oct 5, 2008 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    Chances are you're going to want to check the balance before performing that query so you can react to insufficient funds/points/wtc errors.

    Or, switch to a signed int.

    It happens because the bytes used to store the numbers are read differently in signed VS unsigned. the bits that would indicate -1 in an signed int would indicate the upper bounds of an unsigned. Hence the reason you can store twice as many positive numbers in an unsigned in than a signed one.
     
    joebert, Oct 5, 2008 IP