Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way I can execute a PHP script from MySQL?

Tags:

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.

like image 852
Manish Basdeo Avatar asked Dec 26 '12 11:12

Manish Basdeo


People also ask

Can I run PHP in MySQL?

In order to connect a MySQL database to PHP, you require MySQL on your computer, a tool to create and manage databases, and PHP installed. The most popular ways to connect a PHP script to MySQL are MySQli and PDO.

How do I execute PHP that is stored in a MySQL database?

Instead of having PHP code in db you could have just a class name that has method called, say, execute() . Whenever you need to run your custom PHP code just instantiate the class of name you just fetched from db and run ->execute() on it.

Can I run PHP in SQL?

Yes, you can. It depends on which version of PHP you're using, but if you're using PHP5+ you can use Microsoft's SQL Server Driver for PHP. Make sure you use version 2, which gives you the PDO functionality as well as the procedural style.


2 Answers

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

like image 92
Mark Baker Avatar answered Dec 20 '22 19:12

Mark Baker


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

like image 31
daniel__ Avatar answered Dec 20 '22 19:12

daniel__