Jump to content

help with this code.


seany123

Recommended Posts

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??

Link to comment
https://forums.phpfreaks.com/topic/143314-help-with-this-code/
Share on other sites

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());
}
?>

Link to comment
https://forums.phpfreaks.com/topic/143314-help-with-this-code/#findComment-751648
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.