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
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";

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.