Saturday, February 12, 2011

PHP generating XML, time-outs randomly

I have no idea. This causes seemingly random time-outs. These in turn break the flash that i am loading it into. Has anyone seen anything like this before?

<?php

require_once("../includes/class.database.php");
require_once("../includes/dbConnectInfo.inc");
require_once("../includes/functions.php");

include("../includes/conn.php");

$cat = $_GET['category'];

$result = mysql_query("SELECT * FROM media WHERE related_page_id=4 && type='copy' ORDER BY id ASC LIMIT 6");


$media = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$media .= "<content>\n";
while($row = mysql_fetch_array($result))
{
 $media .="<member>\n";
 $body = $row[copy];

 if($row[title] == "")
  {
   $media .= "<title><![CDATA[";
   $media .= "Team";
   $media .="]]></title>\n";
  }elseif($row['path']==""){

   $name = explode("/",$row[title],2);

   $media .= "<name><![CDATA[";
   $media .= $name[0];
   $media .="]]></name>\n";

   $media .= "<job><![CDATA[";
   $media .= $name[1];
   $media .="]]></job>\n";
  } 

 if($body !="")
 {
 $media .="<bio><![CDATA[";
 $media .= $body;
 $media .= "]]></bio>\n";
 }

 $something = $row['id'];
 $result1 = mysql_query("SELECT * FROM media WHERE assets='$something'");
 $media .= "<images>";
 while($row1 = mysql_fetch_array($result1))
 {

  $img = explode("/",$row1[path],2);
  $media .= "<image url='$img[1]' />";

 }
 $media .= "</images>\n";
 $media .="</member>\n";
} 
$media .= "</content>";
echo $media;

?>
  • What happens if you add set_time_limit(0); to the code? I usually add that line to long-executing code below the include statements.

    Since this works, let me elaborate.

    By default, PHP scripts are set up to only execute for so long. I believe the limit is 30 seconds when PHP is installed, but this can be changed in the php.ini file. However, you might not want to allow all scripts to run for the same length of time - 30 seconds might be fine for most scripts. The set_time_limit function allows you to set the execution time for one script. It takes an integer value which represents the number of seconds to run the script, but a value of 0 means the script will run forever.

  • The default timeout for PHP is 30 seconds. If that code is taking 30 seconds then I think you have some problems! Do you have indices on the media table (one across related_page_id + type and one on assets should do it).

    Increasing the time limit is more like hiding the problem than fixing it, and I definitely wouldn't use set_time_limit(0);. You want some kind of sensible maximum in there, be it a minute, an hour, a day or whatever, depending on your needs.

    Thomas Owens : But there is some code that you do want to run to completion. We have a database of 30k records and counting that we check monthly. We don't want that to stop for any reason, so an infinite time limit is appropriate.
    Greg : Do you really mean that? Infinite time means more than a month... have you tested it with two (or more) copies of the script running simultaneously? What's the performance impact of having it running 24/7? I still think you'd be better off taking the max time it runs for and multiplying by eg 10.
    From Greg

0 comments:

Post a Comment