0

I have a php/mysql question:

I am designing a website that takes xml feeds and puts them into a mysql table. From that table, I want mysql to take this new information and perform a series of calculations which then changes fields in another part of the database. This newly updated information is then displayed on the website.

I understand how to:

  • convert from xml to php
  • place these variables into mysql
  • calculate the new info (in theory with php)

What I don't get is: where do I put the php script that tells mysql to do the calculations and what the calculations actually are.

Does it just go in a php file on the database that gets called by a cron job? I have no idea how to do linux commands though I suppose I could learn if needed. Is there any other way to simply put a script on a server and have it called every hour or so and it checks to see if any new tables have been added and if so, it does the calculations?

1
  • 1
    I think the solution you mention is best; cron is the easiest way to have a script executed periodically. Commented Aug 24, 2011 at 20:14

5 Answers 5

3

Yes, a cronjob is what you're looking for. Depending on your server OS flavor (I'm assuming linux), creating one is fairly easy. On a Debian system, you'd put your script file in /etc/cron.hourly/ and it would be run once every hour.

To create a php script that can be run from the command line, follow this format:

#!/usr/bin/env php
<?php
  // do stuff...
?>

And don't forget to chmod +x the script.

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

Comments

1

If you wanted to schedule a job, without using a cron, you could theoretically set up one of your pages to issue an AJAX request.

AJAX call fired on document ready:

$.get("my-script.php");

Top of myscript.php:

$desiredUpdateInteval = 3600; #seconds
    $dbLastUpdatedQuery = mysql_query("SELECT UPDATE_TIME FROM information_schema.tables WHERE TABLE_SCHEMA = '*database_name*' AND TABLE_NAME = '*table_name*'");
    $dbLastUpdated      = strtotime(mysql_result($dbLastUpdatedQuery,0,0));

    if((time() - $dbLastUpdated) > $desiredUpdateInterval){
    /* do something */
    }

You should note that there's a lot of overhead with this method, as your database is going to get pinged on each page visit.

1 Comment

+1 very good approach in part II (UPDATE_TIME FROM information_schema.tables) !! I just wouldn't like to have the timing controlled by user activity, when the php call is placed in the page.
0

what do you mean by "a website that takes XML"? If xml is taken by a php script of your own, then you should place your re-calculation-call there ... if there is new XML saved to DB. Otherwise you know, that no calculation is needed.

plcae a timestamp attribute "last_updated" in your calculation-results-table, update it if new xml arrived and check on now() > last_updated + 3600, to check if an hour is exceeded.

--> PHP pseudo-cronjob...

Comments

0

You have a couple choices here. If you want to keep things in the db then you can look at using triggers. Trigger will allow you to do things when something like an update/insert takes place. So in your scenario, when the xml is feed into your db, you can trigger some action to perform calculations without having a cron job call a php file to do the same.

The other option is to do a cron job that calls a php file that does the calculations for you if something has changed. You would have to start a crontab (some examples here - search for php) that calls a php file (you should be able to place the file anywhere you like that you have access too, I would keep it in the site folder but outside of public access unless you want to manually call it by going to a url) every x hours depending on your needs. If you do this, you create a new php file which only needs to check the db for changes, maybe by getting the last id of a table and compare it to the previous id that you saved in a file on the file system. If there is a change, do all the required computations and updates and then it's done until it gets called again later.

Comments

0

What I don't get is: where do I put the php script that tells mysql to do the calculations and what the calculations actually are.

Automatic = Cron. Because without it, someone need to execute the script either via browser or CLI.

Now, assume you already have a php script to do the task, and assume you have a ssh access to your server, to set up a cron task you just need to execute (from your bash)

crontab -e

then set up a cron task (assume for every one hour)

*/60 * * * * /path/to/your/php/script.php

If you dont have a ssh access, and using cPanel, see How To using Cron in cPanel

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.