Drew
Drew

Reputation:

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;
?>

Upvotes: 2

Views: 210

Answers (2)

Thomas Owens
Thomas Owens

Reputation: 116179

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.

Upvotes: 3

Greg
Greg

Reputation: 321698

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.

Upvotes: 2

Related Questions