Jump to content

How to set the LIMIT Dynamically


jagularen

Recommended Posts

Hi,

 

This line of code works just fine for me:

$sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT 0, 30';

 

I want to be able to set the integer values dynamically and I thought something like this would work:

$sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT' . $my_int_value . ',' . $my_int_value + 30;

 

But it doesn't... Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/252081-how-to-set-the-limit-dynamically/
Share on other sites

Your original issue is that you need some white-space after the LIMIT keyword.

 

The additional single-quote that Spring added on the end resulted in an odd number of quotes, which would be a php syntax error.

 

Also, the second number in the LIMIT clause is not the ending row number, it is the number of rows, so, it will remain 30 (unless you want to change the number of rows.) Only the the first number, the starting row number, changes.

 

It's usually less error prone to use overall double-quotes when building a query. You can then put php variables directly inside the query string -

 

$sql_quest = "SELECT id, username FROM user ORDER BY id ASC LIMIT $my_int_value,30";

Thanks guys!

 

I forgot the brackets at the end...

 

$sql_quest = 'SELECT id, username FROM user ORDER BY id ASC LIMIT' . $my_int_value . ',' . ($my_int_value + 30);

 

I'm new here and when I solved this yesterday I was trying to reply to my own last reply to say that I solved it or mark the post as solved.

How can  I do this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.