如何检索MySQL事件插入的记录的id

时间:2015-04-13 12:29:25

标签: php mysql scheduled-tasks

以下是我的代码:

$schedule = $this->db->query("CREATE EVENT ".$data['id']." ON SCHEDULE 
EVERY 1 MONTH ON COMPLETION PRESERVE ENABLE DO INSERT INTO oops_invoice 
(id, prefix, number, bill, account_id, order_delivery_id, date, staff_id, 
customer_note, recurrence, status, itp, utp) VALUES ('', '".$data['prefix']."', 
'".$data['number']."', '".$data['bill']."', '".$data['account_id']."', NULL, 
'".$data['date']."', NULL, '".$data['customer_note']."', '".$data['recurrence']."', 
'Draft', '".$data['itp']."', CURRENT_TIMESTAMP)");

$this->db->query("SET GLOBAL event_scheduler =  'ON'");

我坚持认为如何检索由MySQL事件插入的id od行。

我试过以下但是当桌子是空的时候没有用...

下面的代码工作正常,除了一个表是空的情况....当时它什么也没有返回,所以我被卡住了......

这个与插入使用MySQL事件插入的简单数据不一样,因此 $this->db->insert_id();无法正常工作 ......

$last = $this->db->query('SELECT MAX(id) AS `maxid` FROM `oops_invoice`')->row()->maxid;

2 个答案:

答案 0 :(得分:1)

无需检索身份证明, 您可以在创建此事件之前1或2分钟开始活动...... 它将消除表中的2个插入。

CREATE EVENT ".$data['id']." ON SCHEDULE EVERY 1 MONTH STARTS DATE_SUB(NOW(),INTERVAL '0:01' HOUR_MINUTE) ON COMPLETION PRESERVE ENABLE DO INSERT INTO oops_invoice (id, prefix, number, bill, account_id, order_delivery_id, date, staff_id, customer_note, recurrence, status, itp, utp) VALUES ('', '".$data['prefix']."', '".$data['number']."', '".$data['bill']."', '".$data['account_id']."', NULL, '".$data['date']."', NULL, '".$data['customer_note']."', '".$data['recurrence']."', 'Draft', '".$data['itp']."', CURRENT_TIMESTAMP)");
    $this->db->query("SET GLOBAL event_scheduler =  'ON'");

答案 1 :(得分:0)

$this->db->lastInsertId()

必须是您的解决方案