Help: Simple array/insert php...

Discussion in 'PHP' started by Skillman13, Nov 18, 2009.

  1. #1
    I have a table -'Zombie' with users in... They have a certain amount of 'Money' I want this page to update their money by + 15, every time the page is ran (Will be a cron job) But, I cant get it to work...
    The 2nd user gets the same '$newmoney' as the first user -So in the MySQL database they have the same money -which is incorrect...

    Can anyone fix this code so it works? Or adapt it? -Or show me a better way to do it... (I'm sort off new to php)

    $query = ("SELECT * FROM `Zombie`");
    $result = mysql_query($query);
    $numrows= mysql_num_rows($result);
    $count = 0;
    while ($count < $numrows) {
    $count = $count + 1;
    $query = ("SELECT * FROM `Zombie` WHERE `Id` = '$count' ");
    $row = mysql_fetch_array($result);
    $money = $row['Money'];
    $newmoney = $money + 15;
    $result = mysql_query($query);
    mysql_query("UPDATE Zombie SET `Money` = '$newmoney' WHERE `Id` = '$count'");
    }

    Thanks,

    James
     
    Skillman13, Nov 18, 2009 IP
  2. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #2
    Basicly

    mysql_query("UPDATE Zombie SET `Money` = 'Money+15'");

    should do what you want..
     
    n3r0x, Nov 18, 2009 IP
  3. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know, but how does MySQL know which username to update? -or does it know from the array? =/
     
    Skillman13, Nov 18, 2009 IP
  4. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #4
    You don´t need an array.. mysql will calculate Money+15 for all users just by using that query.. you don´t need any other code for that.
     
    n3r0x, Nov 18, 2009 IP
  5. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok great thanks :) -So what will i replace, with that line?
     
    Skillman13, Nov 18, 2009 IP
  6. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #6
    all of this

    
    $query = ("SELECT * FROM `Zombie`");
    $result = mysql_query($query);
    $numrows= mysql_num_rows($result);
    $count = 0;
    while ($count < $numrows) {
    $count = $count + 1;
    $query = ("SELECT * FROM `Zombie` WHERE `Id` = '$count' ");
    $row = mysql_fetch_array($result);
    $money = $row['Money'];
    $newmoney = $money + 15;
    $result = mysql_query($query);
    mysql_query("UPDATE Zombie SET `Money` = '$newmoney' WHERE `Id` = '$count'");
    }
    
    PHP:
     
    n3r0x, Nov 18, 2009 IP
  7. Skillman13

    Skillman13 Peon

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Haha ok thanks -working now :)

    Never new you could update more than one row with one query :) -Now I know. thanks :)
     
    Skillman13, Nov 18, 2009 IP