我要检查用户团队发布的1天后未被排除的挑战,然后自动退还用户的团队,然后删除所有挑战。到目前为止,这是我的代码。
//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()) . ")");
}
}
答案 0 :(得分:0)
你需要发布更新的MySQL,删除循环中的条件。
你已经在循环中获得了ids。
所以,最终的代码应该是:
//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()).")");
不要使用mysql_函数,因为它们已被弃用,将在未来的PHP版本中删除。
答案 1 :(得分:0)
无需移动ID(使用select获取id到客户端代码,然后使用IN
子句创建查询),您可以使用正确的SQL一次更新和删除。有点像:
$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");
}
答案 2 :(得分:0)
$ arrayin = array();
$ autorefund = mysql_query(“SELECT * FROM challenges
WHERE a
=”。$ team ['id']。“”
。 “AND accepted
= 0 AND completed
= 0 AND chtype
= 1 AND(expires
&lt;”。((int)time())。“)”);
if(mysql_num_rows($ autorefund)&gt; 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()) . ")");
}
答案 3 :(得分:-1)
$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).")");
}