查询有很多结果行但是当我遍历这些行时,我只得到两行

时间:2012-10-18 17:45:58

标签: php mysql

我的网站提供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;
?>

1 个答案:

答案 0 :(得分:1)

因为

for ($i=0; $i < $num_results; $i++) { 
$row = mysql_fetch_assoc($results);  

此处$row仅分配了一个结果,因此请使用如下

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>' . utf8_decode($row['resource']) . '</description>';
    $rssfeed .= '<guid>'. $siteurl.'/article.php?id='.$row['id'] . '</guid>';
    $rssfeed .= '<pubDate>' . $artdate . '</pubDate>';
    $rssfeed .= '</item>';

  }

使用mysql_*函数也不建议使用PDOMYSQLi代替

Here is good pdo tutorial