April 3rd, 2005, 06:24 PM
Calling python program from mysql or php?
I am new to python and I was wondering if anyone know how to call a python program from mysql or php.
I have a php web application that will update a mysql database, once the mysql database is update--I either want the mysql db to call the python program or perhaps have php do it. Right now, I have a standalone python program and I want it to interact with mysql or php in some way. The only other thing I can think of is have the python program always running and polling the database for update but that seems awkward. What would be the best method for me to accomplish what I need?
I am using python 2.4.2, php 4 and mysql 4.1 on a windows environment with apache 1.3.
Any suggestion would be appreciated.
April 4th, 2005, 01:23 AM
I don't think you can get MySQL to execute programs unless you want to look into writing a user defined function (and IIRC these can only be done in C/C++). Although you might be able to write a very simple wrapper which executes the python program (can anyone else correct me here?).
You should be able to use php's exec() or passthru() to execute a python program.
If the php script is updating the database, then you should be able to use exec/passthru to run the python program if the mysql insert worked (easy to check in php, just use or die() on the query).
If you don't know when the database is being updated, then maybe you could use cron or at to poll the database, say, hourly or whenever and run the python program if the desired data is there (or update the output whatever).
Comments on this post
April 4th, 2005, 07:44 AM
Nice post Simon. You should also be able do this using SOAP to interact with the Python program from PHP however this is probably overkill .
PHP has a whole bunch of functions for interacting with external programs, you can find a list of these by searching the PHP web-site.
You might get more responses asking this in the PHP forum. I'm going to this thread there, this not really being a Python question .
Take care guys,
April 5th, 2005, 04:35 AM
Thanks for the reply and the information Simon! Much Appreciated.