/    Sign up×
Community /Pin to ProfileBookmark

How To Grab Rows Inbetween 2 Sets of Numbers ?

Php Guys,

How would you do a query so php only grabs mysql tbl rows 100-200 ? Imagine tbl is called ‘users’ and you want to grab data from column ‘username’. How to do this ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 15.2019 — You never grab by "row" -- you grab by some column. In this case, you probably grab by whatever the primary key column is, which I'll pretend is "id" for now:
<i>
</i>select *
from my_table
order by id
limit 100 offset 100
;

Look up "order by", "limit" and "offset" in the MySQL manual if that does not make sense to you.
Copy linkTweet thisAlerts:
@site-developerauthorMar 15.2019 — @NogDog#1601821

Thanks.

Let's say, I got total 1000 rows and I want each pagination page displaying 100 rows each. In that case, when the user is shown the 1st pagination page then I should code the query like this following, right ?
<i>
</i>select *
from my_table
order by id
limit 100 offset 0
;


And when user clicks the 2nd page in the pagination then I get the 2nd page to make query like the following, correct ?
<i>
</i>select *
from my_table
order by id
limit 100 offset 100
;


Then get the 3rd page to query like ....
<i>
</i>select *
from my_table
order by id
limit 100 offset 200
;


Yes ?

On each page, I will have to query mysql again limiting the query to grab 100 rows and I give it the starting point for that particular page with the offset. Correct ? The offset gets raised by the limit per page or results per page number (in our case 100), right ?

If this is how you do it then I might aswell attempt my own shorter way of building a pagination page with mysqli using procedural style. No need for me to waste my nights again googling for a tutorial. I won't forget to use the ceil for rounding-up the numbers and I won;t forget to build a variable that calcs how many pages should be holding the total results in the pagination. So, don't worry about that.

However, if I'm missing anything then do speak up!

Cheers!
Copy linkTweet thisAlerts:
@site-developerauthorApr 11.2019 — @nogdog, I will appreciate a reply from you on my previous post.
Copy linkTweet thisAlerts:
@NogDogApr 11.2019 — That's the server-side-driven way to do it. The alternative is doing it on the client side, sending the entire result set to the page and allowing JavaScript (via one of the available pagination libraries, most likely) to handle the pagination. Either way has advantages and drawback, and I'm not suggesting either is better than the other.
×

Success!

Help @site-developer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...