SELECT然后立即删除mysql记录 [英] SELECT then immediately DELETE mysql record

查看:91
本文介绍了SELECT然后立即删除mysql记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行SELECT查询的PHP脚本,然后立即删除该记录.有多台计算机ping相同的php文件并从同一表中获取数据.每个远程计算机都在cron作业上运行.

I have a PHP script that runs a SELECT query then immediately deletes the record. There are multiple machines that are pinging the same php file and fetching data from the same table. Each remote machine is running on a cron job.

我的问题是,由于某些计算机在同一时间ping正常操作,因此有时无法足够快地删除它.

My problem is that sometimes it is unable to delete fast enough since some of the machines ping at the exact same time.

我的问题是,我如何从数据库中选择一条记录,并在下一台计算机将其删除之前将其删除.目前,我只是添加了一个短暂的延迟,但是效果不是很好.我尝试使用交易,但我认为它不适用于此.

My question is, how can I SELECT a record from a database and have it deleted before the next machine grabs it. For right now I just added a short delay but it's not working very well. I tried using a transaction, but I don't think it applies here.

这是我的脚本的示例片段:

Here is an example snippet of my script:

<?php

$query = "SELECT * FROM `queue` LIMIT 1";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
    $email = $row['email'];
    $campaign_id = $row['campaign'];
}

$queryx = "DELETE FROM `queue` WHERE `email` = '".$email."'";
$resultx = mysql_query($queryx) or die(mysql_error());

?>

非常感谢您的帮助.

推荐答案

将您的删除查询放入while循环中,以防万一您想从选择中增加限制.

Put your delete queries inside the while loop, just incase you ever want to increase the limit from your select.

<?php
$query = mysql_query("SELECT * FROM `queue` LIMIT 1") or die(mysql_error());

while($row = mysql_fetch_array($query)){
    mysql_query("DELETE FROM `queue` WHERE `email` = '" . $row['email'] . "' LIMIT 1") or die(mysql_error());
}
?>

上面的代码与运行相同:

The above code would be just the same as running:

mysql_query("DELETE FROM `queue` LIMIT 1") or die(mysql_error());

请谨慎使用删除查询,如果电子邮件字段为空白,它将删除所有具有空白电子邮件的行.将LIMIT 1添加到您的删除查询中,以避免删除多行.

Be careful using your delete query, if the email field is blank, it will delete all rows that have a blank email. Add LIMIT 1 to your delete query to avoid multiple rows being deleted.

要添加随机延迟,您可以在顶部添加 sleep

To add a random delay, you could add a sleep to the top of the script,

例如:

<?php
$seconds = mt_rand(1,10);
sleep($seconds);
?>

这篇关于SELECT然后立即删除mysql记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆