-2

I am trying to insert data from feed into the database using Zend and also adding a column date_updated in table so that whenever the article in feed is updated the column also gets updated. But the problem is, the first article is being inserted earlier and thenafter the others. So, when I am trying to do a select of top 10 articles based on date_updated DESC, the article being inserted at last is getting on top and if i use ASC then the older ones are getting selected. Please suggest how do i proceed. The query I am writing is:

$sql = "INSERT INTO news_article
    (original_article_id, headline,summary, keywords, link, section, topic, date_published, date_updated, content, source_id)
    VALUES (?,?,?,?,?,?,?,?,?,?,?) 
    ON DUPLICATE KEY UPDATE 
        original_article_id = ?,
        headline = ?,
        summary = ?,
        keywords = ?,
        link = ?,
        section = ?,
        topic = ?,
        date_published = ?,
        date_updated = ?,
        content = ?,
        source_id = ?";
$values = array(
    "original_article_id"=>$id,
    "headline"=>$item->title,
    "summary"=>$summary,
    "keywords"=>$keywords,
    "link"=>$item->link,
    "section"=>"property",
    "topic"=>"property",
    "date_published"=>$formattedPubDate,
    "date_updated"=>$currentDate,
    "content"=>$data,
    "source_id"=>"3"
);  
$result = $db->query(
    $sql,
    array_merge(array_values($values), array_values($values))
); 

and thenafter i am using

SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 10
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • So what datatype is `date_updated`? Is the problem with the INSERT/UPDATE not setting the correct date, or with the SELECT? Narrow down your problem – Mark Baker Sep 24 '13 at 10:16
  • WHat data type are your date fields? What data are you putting in them? For this to work you need to use `DATE` OR `DATETIME` fields. –  Sep 24 '13 at 10:17
  • The datatype is DATETIME fot date_updated. The problem is that while inserting into table, the article which is at the top in the feed is being fed earlier than the ones which have come late. So, when i do a select, the article which was at lastis coming first which should not happen – user2810532 Sep 24 '13 at 10:40

1 Answers1

0

use below query

SELECT * FROM news_article ORDER BY date_updated DESC LIMIT 0,10

After limit we need to pass offset and number of records to fetch.

Gyaneshwar Pardhi
  • 471
  • 1
  • 4
  • 15