Why the conversion from timestamp time() to a string representing a date, and then from date string to timestamp?
#$now = strtotime(date("d/m/Y H:i:s"));
$now = time(); # same thing...
Why the while loop? If you get more than one row, $billing will contain the value of the last row only... And I'm guessing you should never get more than one row due to the where clause
Gmans;10962519 wrote:
$result = db_query ($sql);
while ($row=mysql_fetch_object($result)) {
$billing = $row->billing;
$expire = date("d/m/Y H:i:sa",$row->expire);
}
... so I'd rather use this if you want to make really double sure that you only got one row.
if ( ($num_rows = mysql_num_rows($result)) == 0) {
# no user found
}
elseif ($num_rows > 1) {
# bad
}
else {
# no need for while loop
$row = mysql_fetch...
}
Else, why not trust the db (if your where clause is checking against a field set as primary key or one that is otherwise having the unique constraint) and just check that you got your one user, which makes for simpler php code.
if ($row = mysql_fetch...) {
# you got your one row
}
else {
# no user found
}
This is done AFTER you query the database (the initial SELECT). Doesn't matter how long you wait to do it, it will always be done after you query the database initially. And there really is no reason to wait. Remove the sleep.
Gmans;10962519 wrote:
$sql = "update users set billing = '1' where user='".$_SESSION["cur_user"]."'";
mysql_query($sql);
If you simply checked if your update query was successful, you KNOW that billing = 1 in the db
$sql = "update users set billing = '1' where user='".$_SESSION["cur_user"]."'";
if ($result = mysql_query($sql)) {
# success - billing really was set to 1 (for zero, one or several rows)
# to find out how many rows were affected...
$updated_rows = mysql_num_rows($result); # should be 1
}
else {
# your query failed for some reason
}
But, looking at your script, if I interpret the intention correctly, you allready know wether to update billing=1 or not, so why not turn the things around for simplicity
# 1. update ... set billing=1 where
# 2. select ... where ...
# 3. now you have up to date data for the user, including the new billing information.
And finally, why pass an integer value as a string literal? I hope the table field is of a discrete type, i.e. integer of some (small) size, or boolean
Gmans;10962519 wrote:
$sql = "... billing = '1' ...";
$sql = "... billing = 1 ...";