1

下面的脚本可以很好地将不同的 rss 提要插入到 mysql 数据库中,在网站上回显一些项目。但是当我尝试在“mysql_query”中订购和限制时,事情就停止了。我怀疑 ORDER BY 和 LIMIT 放置在错误的位置,但我看到的唯一可能是将它们放入 mysql_query。有谁知道吗?

$feeds = array('https://www.ictu.nl/rss.xml', 'http://www.vng.nl/smartsite.dws?id=97817');
foreach( $feeds as $feed ) {
    $xml = simplexml_load_file($feed);

    foreach($xml->channel->item as $item)
    {
    $date_format = "j-n-Y"; // 7-7-2008
            echo date($date_format,strtotime($item->pubDate));
            echo ' ';
            echo ' ';
            echo '<a href="'.$item->link.'" target="_blank">'.$item->title.'</a>';
            echo '<div>' . $item->description . '<br><br></div>';

    mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate) 
                VALUES (
        '', 
        '".mysql_real_escape_string($item->title)."', 
        '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
        '".mysql_real_escape_string($item->link)."', 
        '".mysql_real_escape_string($item->pubdate)."')")ORDER BY 'title' LIMIT 0,10;       
    }
}
4

3 回答 3

4

ORDER BY 和 LIMIT 不与 INSERT 语句一起使用,它们需要与 SELECT 语句一起使用。

于 2012-04-25T20:25:56.713 回答
0

不要在列名周围使用单引号,如果有的话,请使用 ` 来包装列名。您的 $selSQL 周围也没有引号,请参见下文并尝试一下:

$selSQL = "SELECT * FROM `rss-feeds` ORDER BY `title`";      
$insSQL = "INSERT INTO `rss_feeds` (`id`, `title`, `description`, `link`, `pubdate`) 
           VALUES (
            '', 
            '".mysql_real_escape_string($item->title)."', 
            '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
            '".mysql_real_escape_string($item->link)."', 
            '".mysql_real_escape_string($item->pubdate)."')"; 

$selres = mysql_query($selsql);
$insRes = mysql_query($insSQL);
于 2012-04-27T19:52:34.410 回答
0

我不确定 mysql_query 和符号。还是有问题。

$selSQL = SELECT * FROM rss-feeds ORDER BY 'title';      
$insSQL = "INSERT INTO 'rss_feeds' (id, title, description, link, pubdate) 
                    VALUES (
            '', 
            '".mysql_real_escape_string($item->title)."', 
            '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
            '".mysql_real_escape_string($item->link)."', 
            '".mysql_real_escape_string($item->pubdate)."'); 

$selres = mysql_query($selsql);
$insRes = mysql_query($insSQL);
于 2012-04-26T18:21:06.947 回答