最近我重建了我的网站,我使用这个简单的代码从mysql db中获取和获取数据为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 =&#34; .$id [.i]。&#34;它改为www.site.com/byt/ [id],其中id = numbers
出现的错误是:
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版本是最新的..
答案 0 :(得分:0)
是否有特殊原因要将项添加到数组中,然后稍后遍历这些数组,而不是直接在记录集迭代中添加rss内容?
此外,不建议使用mysql_*
函数套件,因此建议您迁移到mysqli
- 尽管此处没有实际的SQL注入风险,因为您没有将用户输入作为参数sql。
也就是说,应该进行某种逻辑测试,看看查询是否产生了记录集。
<?php
$connection = mysql_connect("localhost", "site_user", "1212121") or die (mysql_error());
$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>";
if( $rss_query && mysql_num_rows( $rss_query ) > 0 ) {
while( $q_rss = mysql_fetch_array($rss_query) ) {
echo "
<item>
<title>{$q_rss['id']}</title>
<link>{$q_rss['joketitle']}</link>
<description>...{$q_rss['preview']}</description>
</item>";
}
} else {
echo "
<item>
<title>Error</title>
<link></link>
<description>The sql query has failed for some reason</description>
</item>";
}
echo "
</channel>
</rss>";
?>