1
最近我重建我的网站,我就是用这个简单的代码来抓住并获取从MySQL数据库的数据作为RSS:RSS源问题
<?php
$connection = mysql_connect("localhost", "site_user", "1212121") or die (mysql_error()); // 'database_name'
$db = mysql_select_db("site_db", $connection) or die (mysql_error());
$rss_query = @mysql_query("SELECT * FROM jokes where valid = '1' order by id asc LIMIT 0,20");
echo $rss = "<?xml version='1.0' encoding='utf-8' ?>
<rss version='2.0'>
<channel>
<title>site</title>
<link>www.site.com</link>
<description>feed</description>
";
while ($q_rss = mysql_fetch_array($rss_query)) {
$id[] = $q_rss['id'];
$title[] = $q_rss['joketitle'];
$des[] = $q_rss['preview'];
}
$count = count($id);
for ($i = 0; $i <= $count-1; $i ++) {
echo $r = "
<item>
<title>".$title[$i]."</title>
<link>www.site.com/view.php?byt=".$id[$i]."</link>
<description>...".substr($des[$i],0,70)."</description>
</item>
";
}
echo "
</channel>
</rss>";
?>
有关链接:www.site。 COM/view.php?BYT = “$ ID [$ i]。”又改为www.site.com/byt/[id]哪个ID =数字
出现的错误:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/site/public_html/rss.php on line 25
Notice: Undefined variable: id in /home/site/public_html/rss.php on line 32
我的PHP和MySQL版本是最新的..
这个错误通常意味着该查询失败,并在'的mysql_query(...)'但在你的情况下,它可能意味着该连接也未能返回'FALSE'。 – RiggsFolly
请不要使用'mysql_'数据库扩展名,它已被弃用(在PHP7中永远无法使用) 特别是如果你只是在学习PHP,花费你的精力学习'PDO'或'mysqli_'数据库扩展, [这里有一些帮助来决定使用哪个](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in- php),因为您正在进行重写,现在是进行此更改的最佳时机。 – RiggsFolly
在打开PHP标记后立即将文件顶部的错误报告添加到文件(例如'<?php error_reporting(E_ALL ); ini_set('display_errors',1);'看看它是否产生任何东西。 – RiggsFolly