Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I have a weird problem with the

Status
Not open for further replies.

BountyHunter2

Programmer
Feb 12, 2003
10
0
0
GB
Hi, I have a weird problem with the below script. It's a scheduled script that runs every half hour.

99 times out of 100 it runs fine. Takes about 10-15 seconds to complete, and results in about 10k worth of data transfer recorded in my access log.

When it goes wrong however, the end of the script is never reached and the data transfer recored is greater than 1GB!

What the script does is
a)load a local xml file
b) parse it
c) check local mysql db to see if that data is already stored
d) if data is not in the database, then it is added
e) repeat a-d for each xml file

I have no idea what the bug is or where. Does anyone have any suggestions?
Is there any way to limit the script execution time so that if it does encounted problems it gives up?

<?php

include 'library.php';

$data = &quot;&quot;;
$headerdata = &quot;&quot;;
$oldlocalfile = &quot;&quot;;
$remotefile = &quot;&quot;;

function processFeed($remotefile) {
global $logdata;

$oldlocalfile = &quot;httpdocs/data/sportinglife_&quot; . substr(strrchr($remotefile, &quot;/&quot;), 1);
$localfile = &quot;ftp://xxxx:xxxx@35.webmasters.com/&quot; . $oldlocalfile;

$localfilehandle = fopen($localfile,&quot;r&quot;);
$data = &quot;&quot;;
while(!feof($localfilehandle)) {
$buffer = fgets($localfilehandle,4096);
$data = $data.$buffer;
}

$parser = xml_parser_create();
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
xml_parse_into_struct($parser,$data,$data_vals,$data_index);
xml_parser_free($parser);

$rows = count($data_index['headline']);

$connection = openDatabaseConnection();

for ($i = 0; $i < $rows; $i++) {
$news_date = gmdate (&quot;Y-m-d&quot;);
$news_time = gmdate (&quot;H:i&quot;);
$news_category = (substr(strrchr($remotefile, &quot;/&quot;), 1)!=&quot;betting.xml&quot;) ? &quot;sport&quot; : &quot;betting&quot;;
$news_subcategory = getSubCategory(substr(strrchr($remotefile, &quot;/&quot;), 1));
$news_headline = mysql_escape_string(strtoupper($data_vals[$data_index['headline'][$i]]['value']));
$news_summary = mysql_escape_string($data_vals[$data_index['summary'][$i]]['value']);
$news_url = mysql_escape_string($data_vals[$data_index['url'][$i]]['value']);
$bettingzone = strpos($news_url,&quot; if ($bettingzone !== false) {
$news_imageurl = &quot;../images/newslogos/bettingzone.gif&quot;;
$news_websiteurl = &quot; } else {
$news_imageurl = &quot;../images/newslogos/sportinglife.gif&quot;;
$news_websiteurl = &quot; }


// check database for this story

$query = &quot;SELECT * FROM News WHERE url='$news_url' AND headline='$news_headline'&quot;;
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
print&quot;<BR>$query<BR>&quot;;
$logdata = $logdata.&quot;\n\n$query\n&quot;;
echo &quot;<BR>num_rows:$num_rows Rows\n&quot;;
$logdata = $logdata.&quot;rows matched:$num_rows\n&quot;;

mysql_free_result($result);

// if not in database, INSERT
if ($num_rows==0) {
$query_part1 = &quot;INSERT INTO News (newsdate,newstime,category,subcategory,headline,summary,imageurl,url,websiteurl) &quot;;
$query_part2 = &quot;VALUES ('$news_date','$news_time','$news_category','$news_subcategory','$news_headline','$news_summary','$news_imageurl','$news_url','$news_websiteurl') &quot;;
$query = $query_part1.$query_part2;
print&quot;<BR>$query<BR>&quot;;
$logdata = $logdata.&quot;\n$query\n&quot;;
$result = mysql_query($query) or die(&quot;Query failed&quot;);

}
}

mysql_close($connection);

fclose($localfilehandle);

}


function getSubCategory($feed) {

switch ($feed) {
case &quot;racing.xml&quot;:
$subcat = &quot;horse racing&quot;;
break;
case &quot;football.xml&quot;:
$subcat = &quot;soccer&quot;;
break;
case &quot;formula1.xml&quot;:
$subcat = &quot;formula one&quot;;
break;
case &quot;rugbyunion.xml&quot;:
$subcat = &quot;rugby union&quot;;
break;
case &quot;rugbyleague.xml&quot;:
$subcat = &quot;rugby league&quot;;
break;
case &quot;nfl.xml&quot;:
$subcat = &quot;us football&quot;;
break;
default:
$subcat = substr($feed, 0, strpos($feed, &quot;.&quot;));
}

return $subcat;
}

$logfile = &quot;../pl/sportinglife2log.txt&quot;;
$logfilehandle = fopen($logfile,&quot;a&quot;);
$logdata = &quot;&quot;;

print &quot;sportinglife2.php begin : &quot;. gmdate(&quot;Y_M_d H:i:s&quot;) . &quot;\n&quot;;
$logdata = $logdata.&quot;sportinglife2.php begin : &quot;. gmdate(&quot;Y_M_d H:i:s&quot;) . &quot;\n&quot;;

processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;processFeed(&quot;
print &quot;sportinglife2.php end : &quot;. gmdate(&quot;Y_M_d H:i:s&quot;) . &quot;\n&quot;;
$logdata = $logdata.&quot;sportinglife2.php end : &quot;. gmdate(&quot;Y_M_d H:i:s&quot;) . &quot;\n\n\n\n&quot;;
fwrite($logfilehandle, $logdata);
fclose($logfilehandle);


?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top