infinite scroll - mysql limit not working - php

I'm building an infinite scroll for a photo gallery of a website.
It starts with 6 photos loaded and should load 3 more photos every-time the user reach the end of the page.
So, me first mysql is:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 0,6
When the user reach the end of the page, I'm using the following mysql command to add 3 more photos:
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,9
The problem is that it returns 4 records instead of 3 and I have no idea why it happens!
Somebody can help me with that? what am I doing wrong?!

To add three more photos, the second limit would be:
limit 6, 3
The arguments are offset and number of records. You are asking for 9 records starting on the 7th (the offset starts counting from zero).

You should learn more about MySQL.
LIMIT [start, ] count
So, in your second case it will be
LIMIT 6, 3
Here you can learn more about LIMIT: doc

Just write
SELECT * FROM tb_galeriaarte ORDER BY datafoto DESC LIMIT 6,3
2nd digit of LIMIT points quantity but not top limit

Related

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

This question already has answers here:
Pagination using MySQL LIMIT, OFFSET
(4 answers)
Closed 4 years ago.
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....
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
You've to use the OFFSET and LIMIT together like this.
Explanation: The OFFSET value is also most often used together with the LIMIT keyword. The OFF SET value allows us to specify which row to start from retrieving data. By the way you can also omit the OFFSET keyword and use like this LIMIT 0,4 which tells to get 4 rows starting from 0. see more
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 0
SELECT ArticleTitle, PreviewText, PreviewImage
FROM Articles
ORDER BY ID DESC LIMIT 4 OFFSET 5

Selecting every 100 rows from MySQL witha next button in PHP

I am looking to implement this code into a site but I have a few questions. What does the code mean exactly? How does it translate? "end as x" what is it doing here? does anyone have a recommendation as to how to best use it with php? I found this script on PHP and asked the creator but I want to ask the community to see if I get a quicker response.
To better clarify, I am trying to select every hundred rows. I am trying to create a next button that will pull every 100 rows with every click of the next button or arrow. Like a pagination but without the number of the pages.
SELECT * FROM storeCoins WHERE intId in (SELECT CASE(intId%100=0) WHEN 1 THEN intId else 0 END AS X FROM storeCoins ORDER BY 'year' ASC
I don't understand what the syntax means but would really like to understand this. Seems very efficient. If you can recommend how to best implement this using PHP that would be very helpful. I created a next button that only shows the next id in the database but not the next 100. My biggest interest is in understanding this sql code.
it can be as simple as...
For page 1 use
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 0, 100
for page 2 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 99, 100
for page 3 us
SELECT * FROM storeCoins ORDER BY 'year' ASC limit 199, 100
and so on

Mysql query - using like <something> and <something='1'> with limit

I am being stumped over this problem, and I can't seem to figure it out, hoping someone here can do make heads or tails over this.
When I use the following to query my database:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%')";
the items show up they way they are supposed to. But when I add limit 10, 5 to the end:
$q = "SELECT * FROM items WHERE (searchable='1' and title LIKE'%".$_srch."%') limit 10, 5";
The query shows up nothing. I have tried everything I can think of, but I must have missed something. Someone help?
Thanks
By using LIMIT 10, 5 you're stating that you want the database to start displaying from the 11th row, and display 5 rows. (11th, 12th, 13th, 14th, 15th) - It might be possible that you actually, don't have enough rows.
Try something like, LIMIT 0, 5 - this will display the first 5 rows from the beginning, but 10, 5 will only work if you have more than 10 rows for the search query.
Read more here: http://www.mysqltutorial.org/mysql-limit.aspx
The Limit clause arguments are offset and number of items respectively.
So the query you write would show 5 rows starting from the 10th row.
How many rows does your original query show. If it is less then 10 rows then the limit query you provide wont work as the number of rows are less.

Mysql & php : Getting the latest X posts, then the next X latest, and so forth

For example, Facebook loads the latest X wall posts, and then if the user scrolls down it loads the next X posts after.
I can see how you can get the first latest 10 wall posts. Something like:
SELECT * FROM wall_posts ORDER BY DATE DESC LIMIT 10
This would return the 10 latest posts. But what if I want the next 10 latest after the first 10, that is the latest 10-20 posts? Thanks.
LIMIT takes one or two arguments. When two arguments are specified, the first one is the offset and the second one is the number of rows to return:
SELECT * FROM wall_posts ORDER BY DATE DESC LIMIT 10, 10

select inbetween elements in mysql?

I am trying to implement the pagination in php. I am using the Mysql as back end database. I am trying to implement the pagination logic.
I would be having lots of record. But the user will see only 10 at a time.
Now to show the first page, i do a
SELECT * from USERS LIMIT 10.
Now to get the next 10 and the subsequent 10 records i am not able to write a query. Please help me fetch the in between records to support pagination logic. Also provide if any other suggestions for pagination.
You should use the OFFSET option.
SELECT * FROM Users LIMIT 10 OFFSET 10 (or 20, or 30);
That way you just pass the start position in the request when you hit next (or the page number) and you'll retrieve the records you want.
MySQL's limit feature can take two arguments:
select * from USERS limit 10,10
The above would retrieve 10 rows starting at row 10. Bear in mind that the MySQL row offset is 0 based, not 1. The first argument is the starting row, the second is the page size.
Also, if your page size is consistent, all you need to do is pass in the current page (default to zero). That would then allow you to specify the start row as a page * size.

Categories