gpt4 book ai didi

php - SimpleXmlElement 无法获取项目

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

我做了一个这样的函数:

function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<h3>".$x->channel->title."</h3><div>";
foreach ($x->channel->item as $entry) {

$title = utf8_decode(stripslashes(mysql_real_escape_string($entry->title)));
$link = utf8_decode(stripslashes(mysql_real_escape_string($entry->link)));
$desc = utf8_decode(stripslashes(mysql_real_escape_string($entry->description)));
$chkexist="SELECT * FROM articles WHERE title='$title' LIMIT 1";
$i =1;


$chkresult=mysql_query($chkexist);
$countresult=mysql_num_rows($chkresult);
if ($title=="") {
echo "No article title, skipping ".$i++."<br>";
}else{
if ($countresult==1) {
echo "The article: ".utf8_encode($title)." Exists. Passing.<br>";
}else {
$sql="INSERT INTO articles (title,link,description,date) VALUES ('$title','$link','$desc',NOW())";
mysql_query($sql) or die("Failed to save articles");
echo "The article: ".utf8_encode($title)." Is saved<br>";
}
}
}
echo "</div>";
}

它可能并不完美,我对这个游戏还很陌生,但是当我试着给它举个例子这个提要时:http://www.tek.no/feeds/general.xml它最终保存但不是所​​有项目,最后给出错误消息:警告:mysql_num_rows() 期望参数 1 是资源,第 29 行 C:\xampp\htdocs\index.php 中给出的 bool 值无法保存文章.

如您所见,我在本地工作。

如果我尝试另一个提要,例如:http://www.dagbladet.no/rss/innenriks/它保存了 4 个项目并在查询中仅给出自定义错误消息,而不是警告:mysql_num_rows() 需要参数.. 等。

知道我在这里做错了什么吗?有没有人有 xampp 或 wampp 来试用这个功能?

最佳答案

实际上,$desc 并没有被转义,我通过 mysql_real_escape_string() 发送了变量;所以在我的查询中我这样做了: $sql="INSERT INTO articles (title,link,description,pubdate,date) VALUES ('".mysql_real_escape_string($title)."','".mysql_real_escape_string($link)."','"‌ .mysql_real_escape_string($desc)."','$pubdate',NOW())";

现在可以了。

关于php - SimpleXmlElement 无法获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15299724/

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