smd
smd

Reputation: 13

Limit and order by a mysql_query

The below script works fairly well to insert different rss feeds into a mysql dbase, echoeing out a few items on a website. But when I try to order and limit in 'mysql_query' things stop to work. I suspect ORDER BY and LIMIT have been placed into the wrong position, but the only possibility I see is to place them into mysql_query. Anybody who knows?

$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;       
    }
}

Upvotes: 1

Views: 422

Answers (3)

Shane
Shane

Reputation: 379

Don't use single quote around column names, if anything use ` to wrap column names. You also didn't have quotes around your $selSQL, see below and try it:

$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);

Upvotes: 0

smd
smd

Reputation: 13

I am not sure about the mysql_query and the notation. Still something wrong.

$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);

Upvotes: 0

Shane
Shane

Reputation: 379

ORDER BY and LIMIT are not used with INSERT statements, they need to be used with SELECT statements.

Upvotes: 4

Related Questions