Search code examples
phpmysqlwebblogs

How to fetch new records by using limit in php mysql?


I am making an infinite scroll app in which records from database will be fetch. When the user clicks "Load More" button a function is called for retrieving data from database.

My problem is that when I am running this(below) query every Time I am getting the same records.

SELECT ArticleTitle, PreviewText, PreviewImage FROM Articles ORDER BY ID DESC LIMIT 4 

regards....


Solution

    • You need to set a offset in hidden field.
    • On clicking load more, you need to get offset, calculate next batch and update offset in same hidden field
    • that new offset, should be passed to query.

    your query looks like

    SELECT ArticleTitle, PreviewText, PreviewImage 
    FROM Articles 
    ORDER BY ID DESC 
    LIMIT 0, 4 
    

    first time offset is 0

    next time it will be 5 (if you are loading 5 records every time)

    then next time it will be 10 and so on