Home > OS >  Limit a range PHP SQL
Limit a range PHP SQL

Time:01-05

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

CodePudding user response:

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();

CodePudding user response:

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
  • Related