Jump to content

script outputs file twice, not sure why


cyberkiller

Recommended Posts

I wrote a small script that will output information to an rss file. It worked fine, however I modified it to output to a randomly named file. Upon running the script it outputs two random files instead of just one. I've narrowed it down and for some reason its the while loop causing the problem. Not sure why though. The table im using just has three fields, id title date  where id is auto incrementing.

 

<?php

$link = mysql_connect('server', 'user', 'pw')
    or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';	
mysql_select_db('database') or die('Could not select database');

$xmldate = date("r");

$query="SELECT * FROM rss order by 'id' desc limit 0,100";

$result=mysql_query($query);

$body="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<rss version=\"2.0\">
<channel>
<title>My Site</title>
<link>http://www.mydomain.com</link>
<description>My Description</description>
<copyright>(c)</copyright>";

$i=0;
while ($i < 100) {

$title=mysql_result($result,$i,"title");

$body .="
<item>
<title>$title</title>
<link>http://www.mysite.com</link>
<description><![CDATA[ my description ]]></description>
<pubDate>$xmldate</pubDate>
</item>";

$i++;
}

$body .="
</channel>
</rss>";

echo $body;

$ran=rand();
$path=$ran.".xml";

$filenum=fopen($path,"w");
fwrite($filenum,$body);
fclose($filenum); 


mysql_close($link);


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.