2016-06-13 57 views
0

我想選擇並僅使用在Cron作業運行和下一次運行之間插入的新記錄。從這個意義上說,我不重複我之前編寫的任何數據或記錄。mysql select語句在cron作業運行後僅選擇新插入的記錄

從下面的select語句中,請有人指示我,謝謝。

// 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']; 

這很重要,因爲數據將用於發送短信,而且我不想向某人發送短信兩次。

請問,有人嗎?

+1

在發送DB短信設置一個標誌,所以cron的檢索尚未發出的短信行,發送短信,然後更新的標誌數據庫 – Tristan

+0

請問,我該怎麼做。 @Tristan –

+0

@BobMyles你有解決方案嗎? –

回答

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"; 
} 
+0

謝謝@Vignesh ....讓我試試看。 –

+0

@BobMyles我也使用CRON作業來檢查新條目。這對我有用。 –

0

如果你想確定你只發送短信一次,那麼在數據庫表中設置一個標誌並在發送短信後更新標誌。

在數據庫表中添加一個字段的標誌:

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發送限制或限制要求。

運行的記錄

$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); 
} 

while循環,如果短信無法發送你可以擴展這個功能來記錄錯誤。

0

assumtion 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') ");