I wan't to check for challenges a user's team posted that have not been excepted after 1 day, then auto refund the user's team back there credits, and then also delete all the challenges. So far here is my code.
//Delete all matches not accepted after 1 day
$arrayin = array();
$autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " "
. "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < " . ((int) time()) . ")");
if (mysql_num_rows($autorefund) > 0) {
while ($autorefund = mysql_fetch_assoc($autorefund)) {
$arrayin[] = $autorefund['id'];
mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " "
. "WHERE `id` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ")");
mysql_query("DELETE FROM `challenges` WHERE `a` IN "
. "(" . mysql_real_escape_string(implode(',', $arrayin)) . ") "
. "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 "
. "AND (`expires` < " . ((int) time()) . ")");
}
}
You need to post the MySQL updated, delete conditions out of the loop.
You are already getting the ids in the loop.
So, the final code should be:
//Delete all matches not accepted after 1 day
$arrayin = array();
$autorefund = mysql_query("SELECT * FROM `challenges` WHERE `a` = " . $team['id'] . " AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")");
if (mysql_num_rows($autorefund) > 0) {
while ($autorefund = mysql_fetch_assoc($autorefund)) {
$arrayin[] = $autorefund['id'];
}
}
mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits'] . " WHERE `id` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ")");
mysql_query("DELETE FROM `challenges` WHERE `a` IN (" . mysql_real_escape_string(implode(',', $arrayin)) . ") AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 AND (`expires` < ".((int)time()).")");
Don't use mysql_ functions as they are deprecated and will be removed in future versions of PHP.
There is no need to move ids around (fetching ids with select to the client code and then creating queries with IN
clause) you can do update and delete in one go with the proper SQL. Something along the lines of:
$now = time();
mysql_query("START TRANSACTION");
$sql = "
UPDATE teams t JOIN challenges c
ON t.id = c.id
SET t.balance = t.balance + c.credits
WHERE c.a = %d
AND c.accepted = 0
AND c.completed = 0
AND c.chtype = 1
AND c.expires < %d";
$sql = sprintf($sql, $team['id'], $now);
$update = mysql_query($sql);
$sql = "
DELETE
FROM challenges
WHERE a = %d
AND accepted = 0
AND completed = 0
AND chtype = 1
AND expires < %d";
$sql = sprintf($sql, $team['id'], $now);
$delete = mysql_query($sql);
if ($update && $delete) {
mysql_query("COMMIT");
} else {
mysql_query("ROLLBACK");
}
$arrayin = array();
$autorefund = mysql_query("SELECT * FROM challenges
WHERE a
= " . $team['id'] . " " . "AND accepted
= 0 AND completed
= 0 AND chtype
= 1 AND ( expires
< " . ((int) time()) . ")");
if (mysql_num_rows($autorefund) > 0) {
while ($autorefund = mysql_fetch_assoc($autorefund)) {
$arrayin[] = $autorefund['id'];
mysql_query("UPDATE `teams` SET `balance` = `balance` + " . $autorefund['credits']. " WHERE `id`='".$autorefund['id']."'");
}
mysql_query("DELETE FROM `challenges` WHERE `id` IN "
. "(" . mysql_real_escape_string(implode(',', $arrayin)) . ") "
. "AND `accepted` = 0 AND `completed` = 0 AND `chtype` = 1 "
. "AND (`expires` < " . ((int) time()) . ")");
}
$query = "SELECT * FROM challenges
WHERE `a` = {$team['id']} AND accepted = 0 AND completed = 0 AND chtype = 1 AND expires < UNIX_TIMESTAMP()";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
$arrayin = array();
while($row = mysql_fetch_assoc($result)){
mysql_query("UPDATE teams SET balance = balance + {$row['credits']} WHERE id = {$row['a']}");
$arrayin[] = $row['id'];
}
mysql_query("DELETE FROM challenges WHERE id IN(".implode(',',$arrayin).")");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.