簡體   English   中英

5分鍾前將消息插入數據庫的功能

[英]Function to get messages inserted into db 5 mins ago

好的,所以當我發送消息時,它將消息插入數據庫。 幾分鍾后,一個webhook運行並將相同的消息插入數據庫

我只想插入一條消息,所以正在編寫這樣的代碼

$response = $this->messages_model->search_messages_five_mins_ago($recivernum,$msg);

if(!$response) {
    $newId = $this->Customer_m->insert_emaildata($add_email);
};

但是創建功能時出現問題。 到目前為止,我有這個

public function search_messages_five_mins_ago($number,$msg)
{
$this->db->select('*');
$this->db->from('messages');
$this->db->where('receiver_num',$number);   
$this->db->where('content',$msg);
// and created_at betteen now() -10 mins and now 
$query = $this->db->get();
return $query->result_array();
}

如果最近10分鍾內插入了消息,我不想再次插入

任何想法請,謝謝

使用簡單的PHP進行救援,您只需要確定datestrtotime即可確定“ 10分鍾前”是什么。

另外,我建議一種更好的方法來確定查詢是否找到了某些東西:

public function search_messages_five_mins_ago($number,$msg)
{
$this->db->select('*');
$this->db->from('messages');
$this->db->where('receiver_num',$number);   
$this->db->where('content',$msg);
$this->db->where('created_at >=', date('Y-m-d H:i:s',strtotime('Y-m-d H:i:s'."-10 minute")));

$query = $this->db->get();

if ($query->num_rows() == 0)
{
    // nothing found. You can do whatever you'd normally do
    return false;
}

else
{
    return $query->result_array();
}

這將找到在“現在負10分鍾”之后創建的匹配消息。 如果找不到任何內容,則返回false;如果找到則返回消息。

在您的控制器中,我會使用

if($response == false) {
    $newId = $this->Customer_m->insert_emaildata($add_email);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM