我的网站提要 rss 是通过 php 生成的。
我用来从 mysql 检索帖子的查询是:
$query = "select * from articles where status = 0
order by date desc LIMIT 0, 20";
然后我执行此循环以生成 XML 提要:
$results = mysql_query($query,$connection) or die(mysql_error());
$num_results = mysql_num_rows($results);
for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_assoc($results);
$date = strtotime($row['date']);
$artdate = date('D, d M Y H:i:s O', $date);
$rssfeed .= '<item>';
$rssfeed .= '<title>' . utf8_decode($row['title']) . '</title>';
$rssfeed .= '<link>'. $siteurl.'/article.php?id='.$row['id'] . '</link>';
$rssfeed .= '<description>' . utf8_decode($row['resource']) . '</description>';
$rssfeed .= '<guid>'. $siteurl.'/article.php?id='.$row['id'] . '</guid>';
$rssfeed .= '<pubDate>' . $artdate . '</pubDate>';
$rssfeed .= '</item>';
}
但是最终生成的 xml 只有最新的两个帖子。
思考时,查询时限制为 20 个。
有趣的是,如果我设置 LIMIT 0, 1 最终提要只有 1 个帖子,因为它应该是。但是,如果我将其提高到 20,它只有 2 个帖子,而且我在该查询上有两个以上的帖子。
请问这是怎么回事?
完整的php代码:
<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
$cat = $_GET['cat'];
include ('config.php');
$sitequery = 'select * from settings;';
$siteresult = mysql_query($sitequery,$connection) or die(mysql_error());
//Create site settings variables
$siteinfo = mysql_fetch_array($siteresult);
$sitetitle = $siteinfo['title'];
$siteurl = $siteinfo['url'];
$logo = $siteinfo['logourl'];
$statcode = $siteinfo['statcode'];
if($cat) {
$query = "select * from categories where id = ".$cat;
$results = mysql_query($query,$connection) or die(mysql_error());
$info = mysql_fetch_assoc($results);
$cattitle = htmlspecialchars($info['name']);
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>'.utf8_decode($cattitle).' - '.$sitetitle.'</title>';
$rssfeed .= '<link>'.$siteurl.'</link>';
$rssfeed .= '<description>'.$sitetitle.' RSS FEED</description>';
$rssfeed .= '<language>pt-br</language>';
$rssfeed .= '<copyright></copyright>';
} else {
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>'.$sitetitle.'</title>';
$rssfeed .= '<link>'.$siteurl.'</link>';
$rssfeed .= '<description>'.$sitetitle.' RSS FEED</description>';
$rssfeed .= '<language>pt-br</language>';
$rssfeed .= '<copyright></copyright>';
}
if (!$cat){
$query = "select * from articles where status = 0
order by date desc LIMIT 0, 20";
} else {
$query = "select * from articles where status = 0
and categoryid = ".$cat." OR parentid = ".$cat."
order by date desc LIMIT 0, 20";
}
$results = mysql_query($query,$connection) or die(mysql_error());
$num_results = mysql_num_rows($results);
/* for ($i=0; $i < $num_results; $i++) {
$row = mysql_fetch_assoc($results); */
while ($row = mysql_fetch_assoc($results))
{
$date = strtotime($row['date']);
$artdate = date('D, d M Y H:i:s O', $date);
$rssfeed .= '<item>';
$rssfeed .= '<title>' . utf8_decode($row['title']) . '</title>';
$rssfeed .= '<link>'. $siteurl.'/article.php?id='.$row['id'] . '</link>';
/*
$rssfeed .= '<description>' . htmlspecialchars($row['body']) .
'</description>'; */
$rssfeed .= '<description>' . utf8_decode($row['resource']) . '</description>';
$rssfeed .= '<guid>'. $siteurl.'/article.php?id='.$row['id'] . '</guid>';
$rssfeed .= '<pubDate>' . $artdate . '</pubDate>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
?>