homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

XML files and cron job

5+ Year Member

Msg#: 4434383 posted 12:59 pm on Mar 28, 2012 (gmt 0)

I am having a nightmare trying to fix something that has been working until recently, but suddenly stopped working.

I had a price comparison section on my site which read external xml files, inserted the values into the database, and then queried them in various ways.

I have been doing this sith simpleXML and I set up a cron job which worked perfectly. However last week something was done on the server as it stopped working. My webhost set the values to "on" to ensure that external xml files could be read: allow_url_fopenOnOn

I also requested the correct new syntax for the cron job since it no longer seemed valid which was provided to me.

So the methodology for this is:
Open a connection to the database
Drop the table where prices are going to be inserted
Create a new table for the prices to be inserted
Create a foreach loop
Read each xml file with simpleXML and insert the values into the table in the database

The cron job is set to occur every 5 minutes

It creates the table in the database but it doesn't insert the values

The scripting is correct because the values are inserted if I call the file manually

I can insert predefined values using the cron job, but it just wont insert the values from the xml files.

I have tested it over and over again and it is precisely the values from the xml files.

can someone please help! I'm going out of my mind



WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 4434383 posted 1:05 pm on Mar 28, 2012 (gmt 0)

Without seeing any data examples and code examples, only the user with a highly accurate crystal ball is going to have a fighting chance of suggesting something I fear.


5+ Year Member

Msg#: 4434383 posted 1:27 pm on Mar 28, 2012 (gmt 0)

<? #!/usr/local/bin/php -q ?>

require_once dirname(__FILE__) . '/../../' . 'connections/dbconnect.php';
require_once dirname(__FILE__) . '/../../' . 'includes/page-id.php';

$drop="DROP table if exists SiteMonitor";

// create table

$create = mysql_query("CREATE TABLE SiteMonitor (
ID INT(11)
, Name VARCHAR(255) NULL
, Link VARCHAR(255) NULL
, Image VARCHAR(255) NULL
, ImageSmall VARCHAR(255) NULL
, Room VARCHAR(255) NULL
, Players INT(11) NULL
, Game VARCHAR(255) NULL
, Ticket VARCHAR(11) NULL
, Jackpot DECIMAL(11,2) NULL
, Progressive DECIMAL(11,2) NULL
, NextGame INT(11) NULL


//declare pageID as first variable so can extract other datafrom main db in individual monitors
$pageID = "xx-siteID-xx";

$xml = @simplexml_load_file("http://address.of.xml.file");
if ($xml) {
//get the fixed values of the bingo room from the pageID
$info = mysql_query("SELECT * FROM SiteTable WHERE PageID='".$pageID."'")
or die(mysql_error());
$infoNumRows = mysql_num_rows($info);


foreach($xml->ROOM as $rooms ) {
$room1 = $rooms->attributes()->name;
$players = $rooms->attributes()->currentplayers;
$ticket = $rooms->attributes()->cardvalue;
$game = $rooms->attributes()->roomtype;
$jackpot = $rooms->attributes()->jackpot;
$progressive = $rooms->attributes()->progressive;
$nextGame = $rooms->attributes()->seconds;

//character substitution and data formatting takes place here

mysql_query("INSERT INTO SiteMonitor (

or die(mysql_error());


something isn't working under the cron job which does work fine manually, but it's not like I can see any errors since even in the email it sends out.

the cron job line I was given by the host (which does execute fine) is
/usr/local/bin/php -q -f /home/xx-account-xx/public_html/monitor/includes/create-table1.php

im not sure what the -q and -f means?


WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4434383 posted 3:50 pm on Mar 28, 2012 (gmt 0)

The short answer is to check your server's error and cron error logs?

You could do what you probably should have anyway - create some sort of error logging scheme. Create a location for it, make it writable:

$logfile = $_ENV['DOCUMENT_ROOT'] . '/cronlog/mylog.txt';

Note the difference from your rather convoluted way of accessing files in your current script - but if your approach works for you, it works . . .

Then add a function that opens a file, appends to mylog.txt, and closes the file. It would accept only the full path to the logfile and the error string itself, making it a self-contained unit not reliant on globals:

function log_error($log,$err) {
// open $log and append $err to it

Make the function so it reads the file size first, and if it's over something like 100K, overwrite, otherwise append. When a text file gets that large it becomes increasingly hard to read in most text editors.

Then throughout your script, add an " or log_error()". The first thing to remove is the @. @ suppresses errors.

$xml = simplexml_load_file("http://address.of.xml.file") or log_error($logfile, "Could not load the specified XML at $address");

Then you have if $xml, but nothing if "not." simplexml_load_file returns false on failure (which may make the previous error different, more like "could not execute simple_xml")

if ($xml) {
// existing code
else { log_error($logfile, "XML result was empty"); }

Then change your die commands to log_error, since it's a cron no one will see them on die.

$info = mysql_query("SELECT * FROM SiteTable WHERE PageID='".$pageID."'")
or log_error($logfile, "Error selecting: " . mysql_error());

$infoNumRows = mysql_num_rows($info);
if (! ($infoNumRows >0)) { log_error($logfile, "No rows to parse"); }

// Put your insert into a variable, then
mysql_query($query) or log_error($logfile, "Error inserting: " . mysql_error());

An aside, if your $pageID is expected to be an integer, don't quote it. The reason: if quoted, it can be empty,

"SELECT * FROM SiteTable WHERE PageID=''

... and it will still run, it just won't do anything. In any case, put your error logging into place again:

if (is_numeric($pageID) and ($pageID > 0)) {
// do it
else { log_error($logfile, "page ID is not a number greater than zero"); }


WebmasterWorld Senior Member 5+ Year Member

Msg#: 4434383 posted 9:47 am on Mar 29, 2012 (gmt 0)

For testing you should get rid of the @ in front of the simplexml_load_file so you can see the warnings. And check the return value for the xml object if it's really false or not.

if ($xml === FALSE) {

otherwise you may get false positives.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved