/    Sign up×
Community /Pin to ProfileBookmark

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

[code=php]<?

//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”;
}

?>[/code]

is this the best efficant way of doing this, would this go through each row and do the above.

thanks to anyone that helps ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@phantom007May 25.2008 — One suggestion, if you are not using any variables in your echo statement then you should use ' ' instead of " "

Wrong way:

echo "Nothing to update";

Correct way:

echo 'Nothing to update';


This way you can make your code much faster since PHP looks for variables within "" and does not in ''
Copy linkTweet thisAlerts:
@NogDogMay 25.2008 — One suggestion, if you are not using any variables in your echo statement then you should use ' ' instead of " "
[/quote]

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 [i]do[/i] 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.:
[code=php]
echo "This " . $is . " faster.";
echo "This $is slower.";
echo 'This ' . $is . ' probably the same as the first one.';
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 25.2008 — 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).
Copy linkTweet thisAlerts:
@mitchellauthorMay 25.2008 — i want this to be carried out on each result so do i need to do
[code=php]while()[/code]

?
Copy linkTweet thisAlerts:
@NogDogMay 25.2008 — The typical idiom with the mysql functions is:
[code=php]
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
{
// do stuff here with the elements of the $row array
}
[/code]
×

Success!

Help @mitchell spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...