gpt4 book ai didi

php - mysql_fetch_array 加 undefined variable 的 RSS 提要问题

转载 作者:行者123 更新时间:2023-11-30 22:25:51 27 4
gpt4 key购买 nike

最近我重建了我的网站,我使用这个简单的代码以 rss 的形式从 mysql 数据库中获取和获取数据:

<?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] which 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 版本是最新的..

最佳答案

您是否有特殊原因想要将项目添加到数组,然后稍后遍历这些数组,而不是直接在记录集迭代中添加 rss 内容?

此外,mysql_* 函数套件已被弃用,因此建议您迁移到 mysqli - 虽然这里没有真正的 sql 注入(inject)风险,因为您不将用户输入作为 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>";

?>

关于php - mysql_fetch_array 加 undefined variable 的 RSS 提要问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35252920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com