如何使用PHP和MySQL创建RSS


How do a create RSS using PHP and MySQL

我尝试制作一个从SQL数据库加载数据的RSS提要。

当我运行它时,它会把我带到互联网浏览器上的RSS阅读器上,并说"没有内容",或者它会返回一个错误:

此页面包含以下错误:第2行第1列的错误:文档末尾的额外内容以下是第一个错误之前的页面呈现。

我正在使用Dreamweaver制作RSS,并在PHP文件上创建它。

<?php require_once('Connections/mydatabase.php'); ?>
<?php 
header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysql_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "
  SELECT news.post_id, news.title 
  FROM news 
  ORDER BY news.updated 
  DESC LIMIT 10
";
$getRecent = mysql_query($query_getRecent, $mydatabase) or die(mysql_error());
$totalRows_getRecent = mysql_num_rows($getRecent);
while ($row_getRecent = mysql_fetch_assoc($getRecent)){
$rss .= '<item>';
$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';
$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';
$rss .= '</item>';
}
$rss .= '</channel>';
$rss .= '</rss>';
echo $rss;
?>

看看这一行:

$rss .= '<title>' . $row_getRecent['news.title'] . '</title>';

结果数组索引不能包含表名,只能包含列名。替换为

 $rss .= '<title>' . $row_getRecent['title'] . '</title>';

看看你得到了什么。

这条线看起来也很可疑。

$rss .= '<link> 
  http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'] . '
</link>';

检查这是否是单行。否则,如果有换行符,请将其更正为单行。

<?php require_once('Connections/mydatabase.php'); ?>
<?php
//header ("Content-Type:text/xml");
?>
<?php
header('Content-type: text/xml');
$mydatabase = mysqli_connect("http://127.0.0.1","root","au291826","db_cms");
$rss = '<?xml version="1.0" encoding="utf-8"?>';
$rss .= '<rss version="2.0">';
$rss .= '<channel>';
$rss .= '<title>My RSS</title>';
$rss .= '<link>http://localhost/cws_files/</link>';
$rss .= '<description>implementation of RSS with PHP </description>';
$rss .= '<language>ar-sa</language>';
$rss .= '<copyright> RSS </copyright>';
mysqli_select_db($database_mydatabase, $mydatabase);
$query_getRecent = "SELECT news.post_id, news.title FROM news ORDER BY news.updated DESC LIMIT 10";
$getRecent = mysqli_query($query_getRecent, $mydatabase) or die(mysqli_error());
$totalRows_getRecent = mysqli_num_rows($getRecent);
while ($row_getRecent = mysqli_fetch_assoc($getRecent)){
$rss .= print_r($row_getRecent);
$rss .= '<item>';
$rss .= '<link> http://localhost/cws_files/index.php?post_id=' . $row_getRecent['post_id'].'</link>';
 $rss .= '<title>' . $row_getRecent['title'] . '</title>';
$rss .= '</item>';
}
$rss .= '</channel>';
$rss .= '</rss>';
echo $rss;
?>