mysql select语句在cron作业运行后仅选择新插入的记录

时间:2016-06-13 04:00:30

标签: php mysql cron

我想选择并使用仅在Cron作业运行和下一次运行之间插入的新记录。从这个意义上说,我不会重复我之前工作的任何数据或记录。

从下面的选择声明中,有人指导我,谢谢。

// select statement should pick fresh records only after the first cron
$sql = "SELECT name,amount, trans_id, msisdn, time_paid FROM customer";
$result1 = mysqli_query($conn, $sql);
$resultarr = mysqli_fetch_assoc($result1); // fetch data
$name = $resultarr['name'];
$amount = $resultarr['amount'];
$transaction_id = $resultarr['trans_id'];
$date = $resultarr['time_paid'];

这很重要,因为数据将用于发送短信,而我不想向某人发送短信两次。

亲切的,有人吗?

3 个答案:

答案 0 :(得分:2)

如果您想获取新插入数据的详细信息,请在表格中添加新列,例如 add_date 。检查此add_date是否等于当前日期。如果是,您将获得每天新日期的详细信息。您可以使用以下代码。

$today = date("Y-m-d");
$check_new_data = mysql_query("SELECT name,amount, trans_id, msisdn, time_paid FROM 
customer WHERE add_date='$today'");

while($check = mysql_fetch_array($check_new_data))
{
            echo "What ever date you need";
}

答案 1 :(得分:0)

如果您想确定只发送一次SMS,则在db表中设置一个标志,并在发送SMS后更新标志。

在数据库表中为标志添加一个字段:

ALTER TABLE `customer` ADD `smsSent` BOOLEAN NOT NULL AFTER `time_paid`;

仅选择未发送短信的地方:

$sql = "SELECT name, amount, trans_id, msisdn, time_paid FROM customer WHERE smsSent = '0'";

您可能希望为SQL查询添加限制,以避免任何超时或SMS发送限制或限制要求。

在记录上运行while循环

$result1 = mysqli_query($conn, $sql);
while ($resultarr = mysqli_fetch_assoc($result1)) {
    // send the SMS
    /* add your sms sending code here */
    // update the database record so the sms is not sent again in future (assumes primary key is id)
    $sql = "UPDATE customer SET smsSent = '1' WHERE id = '{$resultarr['id']}'";
    $res = mysqli_query($conn, $sql);
}

如果SMS无法发送,您可以将此功能扩展为记录错误。

答案 2 :(得分:0)

假设table_names和列名自行更改

    follow Vignesh Chinnaiyan steps and if you want dont sent message twice means create status table.

    id customer_id  insert_date           status 

     1    1        2016-06-13      sent
     2    2        2016-06-13      sent

     $check_new_data = mysql_query("SELECT name,amount, trans_id, msisdn, time_paid FROM 
     customer WHERE add_date='$today' and  customer_id NOT IN (select customer_id from status_table where insert_date='$today') ");