Please Could you check my code if its the right way
Hi everyone hopefully my code bellow does the following, but wounderd if this is the correct way to go through each row and update each user
PHP Code:
<?
//connect to building database
$result = mysql_query("SELECT * FROM buildup")
or die(mysql_error());
$row = mysql_fetch_array( $result )
//some variables of time
$expiry = $row["endtime"];
$now = time();
$upgrade='bank';
$buildingnumber = $upgrade
//if endtime has passed time update there building
if($expiry < $now){
mysql_query("UPDATE users SET $buildingnumber=$buildingnumber+'1'");
echo "Updated building Successfully";
mysql_query("DELETE FROM buildup WHERE $expiry < $now");
echo "Deleted and updated Successfully";
}
elseif($expiry > $now){
echo "Nothing to update";
}else {
echo "Error! Thats not good";
}
?>
is this the best efficant way of doing this, would this go through each row and do the above.
One suggestion, if you are not using any variables in your echo statement then you should use ' ' instead of " "
Actually, if you do some testing, you'll find there is essentially no difference as long as there are, in fact, no variables to be interpolated within the string. Where you do get some minor time savings (we're talking milli- or more likely micro-seconds here) is using concatenation instead of interpolated variables within the string, .e.g.:
PHP Code:
echo "This " . $is . " faster."; echo "This $is slower."; echo 'This ' . $is . ' probably the same as the first one.';
Last edited by NogDog; 05-25-2008 at 03:09 PM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Your code is missing a couple semi-colons, so as it is now it won't even parse.
Not knowing what your functional requirements are for this code, I'm not sure whether it really does what you want. It appears to retrieve all records from the "buildup" table, but it only reads one row from the result set.
PS: You should always use the "<?php" opening tag instead of "<?", in order to ensure maximum compatibility, avoid confusion with <?xml> tags, and be forward compatible with PHP 6 (which will not even have an option to allow the short tag).
Last edited by NogDog; 05-25-2008 at 03:11 PM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
$result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { // do stuff here with the elements of the $row array }
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks