-1

Following is my code:

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

I stuck with id that how to retrieve id od row inserted by MySQL event..

i have tried following but does not worked when table is empty...

following code works fine except one case when table is empty....at that time it return nothing so i have been stuck...

this one is not same as we insert simple data this one inserted using MySQL event so $this->db->insert_id(); does not working......

$last = $this->db->query('SELECT MAX(id) AS `maxid` FROM `oops_invoice`')->row()->maxid;
Sagar Naliyapara
  • 3,971
  • 5
  • 41
  • 61

2 Answers2

1

No requirement of retrieving id, You can start event before 1 or 2 minutes of it's creation like this... it will eliminate 2 insertion in table.

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'");
0
$this->db->lastInsertId()

must be your solution

DDA
  • 161
  • 6