php - SimpleXmlElement 无法获取项目

标签 php mysql function rss simplexml

我做了一个这样的函数:

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/

相关文章:

mysql - 在 MY-SQL 中一起插入多个不存在的值。其中 "NULL"应该是唯一的

php - php 和 perl 3DES CBC 中的返回值相同

来自 iOS 应用程序的 PHP POST

php - MySQL LAST_INSERT_ID() 和 FOUND_ROWS()

mysql - 如何在SQLite session 中设置时区?

python - 当以 f(x)(y) 格式调用 Python 函数时,这意味着什么?

python - 如何在Python中修复 “TypeError”?

javascript - 这个带有可变引用参数的 JavaScript 函数是纯函数吗?

php - Facebook API : count attendees for event (limit problem)

php - 通过 mail() Apache+PHP5.3+sendmail 发送电子邮件时延迟 20 秒?