20

I want to invoke and execute a PHP script from a MySQL procedure. Is this possible?

CREATE PROCEDURE simpleproc (OUT param1 INT)
    BEGIN
         Call my php script here
    END//

[EDIT]
I am in fact trying to raise an event when a condition is met — for instance when my table field value matches the current time. Then, I want to capture the event and send an email.

5
  • 7
    No You cant!! Share your exact problem we try to find alternative solutions. Commented Dec 26, 2012 at 11:56
  • 3
    No you can't thou you could retrieve php script from the database and call eval function, but whatever you are doinbg it would be plain wrong Commented Dec 26, 2012 at 11:58
  • 1
    Try to look at do_system() in databasesecurity.com/mysql/HackproofingMySQL.pdf Commented Dec 26, 2012 at 12:02
  • 1
    possible dup of stackoverflow.com/questions/2943205/execute-php-via-mysql?rq=1 Commented Dec 26, 2012 at 17:02
  • A cron script is probably better than using a database trigger for this Commented Dec 28, 2012 at 14:05

3 Answers 3

20

It's possible, See the MySQL FAQ for an explanation of how

Can triggers call an external application through a UDF?

But it's likely to be bad design on your part if you have to resort to this

Sign up to request clarification or add additional context in comments.

Comments

12
DELIMITER @@

CREATE TRIGGER Test_Trigger
AFTER INSERT ON MyTable
FOR EACH ROW
BEGIN

DECLARE cmd CHAR(255);
DECLARE result int(10);
SET cmd=CONCAT('/usr/bin/php ', '/home/test/beta/demo.php');
SET result = sys_exec(cmd);

END;
@@
DELIMITER ;

source

1 Comment

Dont have time to test.. Does it really work? Did you test? Looks great!
3

For solve your problem you can create table for tasks. From stored procedure you can put to this table any string for task. On server you can run PHP script by crontab. Script will check this table and make some operation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.