SRL Archive - Web Development

Latest PHP Dilemma


smurg

2011-08-15 11:36:25

Basically, I pull off all the prices and updates of items off the GE database by having my php script load a page with the item name in the url. Then it retrieves the info from the retrieved source.

I have a separate script that redoes this and updates the values in my MySQL database. Basically, with GE updates, I want to automate this so it goes along with GE updates. So I set up a cron job to automate running my update.php every 30min. It wasn't working, so I followed someone elses advice to throw in a set_time_limit in the script to prevent the cron job from closing too early. It still doesn't work though.

I'm at work atm, but it's essentially like this.

$item_price = 100;
$item_shift = 5;

$update = mysql_query('UPDATE Table_Name SET (PRICE='.$item_price.', SHIFT='.$item_shift.') WHERE ID=$item_ID');

I normall include a file in the beginning of my sensitive database (inserting, updating files) to redirect IPs other than my own. Except I tried adding the IPs (localhost, 127.0.0.1, external ip) but it didn't work, so I just left it off for now.

When I path to the update file manually and let it load, it successfully updates all my database info, so I don't know where it's going wrong with the cron job.

Any ideas on values to set in the script? I'll post the cron job command when I get home from work.

Frement

2011-08-15 12:29:25

Example cron:
# Look for and purge old sessions every 30 minutes
09,39 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -delete

superuser

2011-08-15 13:42:30

From: smurg
…

$update = mysql_query('UPDATE Table_Name SET (PRICE='.$item_price.', SHIFT='.$item_shift.') WHERE ID=$item_ID');

…


should be

$update = mysql_query("UPDATE Table_Name SET PRICE=$item_price, SHIFT=$item_shift WHERE ID=$item_ID");

Also, I suggest you to check out PDO. It makes things so much easier and clearer, for instance the previous clause:


$sth = $db->prepare('UPDATE Table_Name SET price=:price, shift=:shift WHERE id=:id');

$sth->bindParam(':price', $item_price);
$sth->bindParam(':shift', $item_shift);
$sth->bindParam(':id', $item_ID);

$sth->execute();

Shuttleu

2011-08-15 13:57:40

From: superuser
should be

$update = mysql_query("UPDATE Table_Name SET PRICE=$item_price, SHIFT=$item_shift WHERE ID=$item_ID");

Also, I suggest you to check out PDO. It makes things so much easier and clearer, for instance the previous clause:


$sth = $db->prepare('UPDATE Table_Name SET price=:price, shift=:shift WHERE id=:id');

$sth->bindParam(':price', $item_price);
$sth->bindParam(':shift', $item_shift);
$sth->bindParam(':id', $item_ID);

$sth->execute();


what he had would have worked perfectly fine

@op there might be something wrong with your cronjob

also with cronjob you need to add a line to the top of your php file

i will find it for you

~shut

EDIT: example cron that i use

0 * * * * /usr/bin/php5 /www/index.php

EDIT: this needs to be at the top of the php
[php]#!/usr/bin/php5[/php]

smurg

2011-08-15 14:24:56

I am with godaddy (seems good so far) and it has software (think it's cPanel) for crons, so I just enter in minimal details, but it shows the final cron command, so I'll post that when I get home.

smurg

2011-08-15 14:40:30

I found this on a website (a php page to simulate a cron job; he said to just remove it from the server when you want to stop it lol):

http://www.maheshchari.com/php-cron-job/

[php]
//This function runs with curl extension that calls or open remote file.
function call_remote_file($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
}

function do_somework(){
//This function to run every minutes
@mail('mahesh@gmail.com','cron job','cron runned');
}
set_time_limit(0);
// some web hosts will have only 30 sec max execution time so we keep it unlimited time
// this function only runs if safe mode is off
$url="http://www.maheshchari.com/cronjob.php"; //it is the script that runs exactly every minutes
call_remote_file($url);
do_somework();//in mean time run some work
sleep(60); // wait the script for 60 seconds or every minute
call_remote_file($url); // call this script itself after a minute
// This process continues every minute
[/php]

What do you think about that? And if I did IP permissions again, what IP should I set? localhost/127.0.0.1 or the servers external ip or ?

Shuttleu

2011-08-15 14:53:55

your best bet is if you can set cronjobs manually

use php to find the executable (/usr/bin/php5)

then do as i said above, but manually do the cronjob instead of the using cpanel

~shut

smurg

2011-08-17 13:34:12

Here's the resulting command it gives me:


/web/cgi-bin/php5 "$HOME/html/****/****.php"

From: Shuttleu
EDIT: this needs to be at the top of the php
[php]#!/usr/bin/php5[/php]


Top of the php file that I want to run the cron for?

Shuttleu

2011-08-17 13:41:10

From: smurg
Here's the resulting command it gives me:


/web/cgi-bin/php5 "$HOME/html/****/****.php"



Top of the php file that I want to run the cron for?


yes

but now it would be
[php]#!/web/cgi-bin/php5[/php]

~shut

EDIT: it needs to be on the very first line