Paulamonopoly
Paulamonopoly

Reputation: 45

Limit a range PHP SQL

Two parts to this question, I currently have a php script which grabs new info from themoviedb, however it times out so I'm trying to limit it in order to chunk it. I have this below snippet.

$Posts = $this->db->from('posts')->where('type','serie')->all();

I am trying to limit it to 250 results as opposed to all, I have tried.

$Posts = $this->db->from('posts')->where('type','serie')->limit(250);
$Posts = $this->db->from('posts')->where('type','serie')->limit('250');

However these don't work, is anyone able to help ?

Also I was looking to see if it's possible to limit a range of results as opposed to a quantity such as limiting to 251 - 500 ?

$Posts = $this->db->from('posts')->where('type','serie')->limit('1','250');

Any help is greatly appreciated!

I have attached a pastebin of the full code.

https://pastebin.com/C3DgFQik

Solution found:

$Posts = $this->db->from('posts')->where('type','serie')->limit(0,0)->all();

Using limit you can set both offset and the limit of results, the first 0 is the offset and the second 0 is the limit.

$Posts = $this->db->from('posts')->where('type','serie')->limit(10,5)->all();

This would retrieve results 11 - 15.

Upvotes: 1

Views: 80

Answers (2)

Muhammad Ahmer
Muhammad Ahmer

Reputation: 112

EditedSo if I am correct you are using Codeigniter , and in that you can use the Limit but after that, you need to use get also like this,

$this->db->from('posts')->where('type','serie')->limit($limit,$start)->get();

Upvotes: 2

ThomasL
ThomasL

Reputation: 930

Would have been cool to know from which library come those function

from()
where()
all()

If you tell me from where your function come, i will update my answer

But to help you find a way , In pure sql language

SELECT * FROM posts WHERE type = "serie" LIMIT 250 

So bases on your code i dont see a "logic" mistake, so its probably link the function limit() itself

To get a proper range, from 251 to - 500 just add an OFFSET

SELECT * FROM posts WHERE type = "serie" LIMIT 250 OFFSET 251

For exemple, for 751 to 1000

SELECT * FROM posts WHERE type = "serie" LIMIT 250 OFFSET 751

Upvotes: 0

Related Questions