#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0

    Calling python program from mysql or php?


    Hi everyone,

    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.
  2. #2
  3. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    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).

    --Simon

    Comments on this post

    • michaelng disagrees : Informative.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    http://uk.php.net/manual/en/ref.exec.php

    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,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Posts
    8
    Rep Power
    0
    Thanks for the reply and the information Simon! Much Appreciated.

IMN logo majestic logo threadwatch logo seochat tools logo