PHP statement executed directly by MySQL

0

Currently, as the sending of the email was giving a lag in the application, because it has to connect with Gmail etc, I transferred to the server the mission to send these messages, as follows: / p>

  • User writes the% s message to the recipient and the% s.
  • On the server I have an open page that gets reloaded every 10 seconds.
  • This page searches the database in tbl_msg , when it finds, it executes a PHP code that sends the email.

This way the application for the user does not suffer lag some.

What I want

Is it possible for the database itself to do something that if any data is written to tbl_msg automatically MySQL execute the PHP statement, without having to have an open page?

    
asked by anonymous 17.09.2014 / 19:21

2 answers

4

There is how you perform external functions in MySQL, but it is tedious and complicated maintenance work.

It's much easier for you to enter the crontab or task scheduler that runs the desired PHP every 10 minutes.

Linux

In most distros * nix the crontab line would look like this, to run every 10 minutes:

5,15,25,35,45,55 * * * * /usr/bin/php meuscript.php > /var/log/meuscript.log

Some distros support this syntax:

*/10 * * * * /usr/bin/php meuscript.php > /var/log/meuscript.log


Windows

Just schedule a task with these features in scheduler . If you want, you can redirect to a log and facilitate debugging and debug .

c:/caminhoparaoseu/php.exe c:/caminho/para/arquivo.php


Independent of OS, Make sure you use the correct paths for things on your system.


PHP Looping

For small intervals of time, you can run a PHP script with an infinite loop at system startup:

<?php
   set_time_limit( 0 ); // para o PHP poder rodar sem limite de tempo

   while( true ) {
       ... aqui vai a sua função do DB ...

       sleep( 10 ); // numero de segundos entre um loop e outro
   }
?>
  

Do not access this script through the browser! Do the command line not to unnecessarily occupy a web server process. In addition, the max_execution_time directive defaults to 0 from the command line, allowing loop to run indefinitely.

    
17.09.2014 / 20:37
2

From what I've seen, it's possible, but it's not simple ...

You need to import the UDF library in MYSQL to use a function called sys_exec .

Then you could create a trigger so that whenever there was a new record, the sys_exec function would be called, and in that call execute a command on the system , which by its run php , passing the script responsible for fetching and sending email as a parameter.

I've never used it, but I've seen it in this question in English . What will validate whether it works or not is your programming skills.

    
17.09.2014 / 19:52