seany123 Posted January 31, 2009 Share Posted January 31, 2009 this code takes away 1 from ($player->prison)..... <?php $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $prison = $row['prison'] - 1; $query = sprintf('UPDATE players SET prison = %d WHERE id = %d', $prison, $row['id']); mysql_query($query) or die(mysql_error()); } ?> but how do i make it so it cant go lower than 0?? Quote Link to comment Share on other sites More sharing options...
phpSensei Posted January 31, 2009 Share Posted January 31, 2009 You can do <?php $query = "SELECT * FROM players"; $result = mysql_query($query) OR die(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $prison = $row['prison'] - 1; if($prison < 0){ $prison = 0; } $query = sprintf('UPDATE players SET prison = %d WHERE id = %d', $prison, $row['id']); mysql_query($query) or die(mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
printf Posted February 1, 2009 Share Posted February 1, 2009 alter the table and set prison as UNSIGNED Also you don't have to use two queries to do this... UPDATE players SET prison = prison - 1 WHERE prison <> 0; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.