John Davidson

mysql trigger executes php file before data is commited

0 comments
Message:


I have a trigger in MySQL that executes a PHP file when a row is inserted, this is the trigger :


AFTER INSERT ON \glpi_tickets \
FOR EACH ROW BEGIN\``

BEGIN

DECLARE result INT;

SET result = (select sys_exec('C:/xampp/php/php.exe C:/xampp/htdocs/lar/query.php'));

END

In that PHP file, I query the last ID using this query :


SELECT MAX(\id\) FROM glpi_tickets;


But I get the ID before the one just inserted even tho the trigger is after insert on, it's like I query then the row is added like I'm using before insert on or the the php file executes before the row is commited either, so to make the php file executes after the row is commited or is there a way to get the row just inserted guys?


I really need this appreciate any help.



My Answer:
The issue you are facing is due to the timing of when the trigger is executed in relation to when the data is committed. Triggers in MySQL are executed before the data is committed to the database, so when your PHP file is triggered, the new row has not been committed yet.

To work around this issue, you can try using a different approach. One option is to use a scheduled task or a cron job to periodically check for new rows in the table and then execute your PHP file. Another option is to modify your PHP file to query for the latest ID after a short delay to allow for the data to be committed.

Alternatively, you can consider using a different database system that supports triggers that are executed after the data is committed, such as PostgreSQL.

Overall, it is important to consider the timing of when triggers are executed and how it may impact your application logic.

Rate this post

4 of 5 based on 9476 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us