0

Hello everyone i would like to ask you why, when i put limit 0,5 and 5,10 it does not work correctly here is my code.

$sql = mysql_query("SELECT * FROM vn_questions WHERE published = 1 limit 0,5 ");
while($data=mysql_fetch_array($sql))
{
    echo $data['author'];
}

it gives me 5 results but when i put

$sql = mysql_query("SELECT * FROM vn_questions WHERE published = 1 limit 5,10 ");
while($data=mysql_fetch_array($sql))
{
    echo $data['author'];
}

it gives me 6 .. where is the problem and how Can i fix it? I tried +1 -1 but the output is not working correctly.

3
  • 2
    5-6-7-8-9-10 = "6" instances. 0 is none, then you start counting up to 5 = 5 --- What is your expected result? Commented Jan 8, 2014 at 23:03
  • If you don't want to set a limit, then leave LIMIT out, or drop the first number. I.e.: LIMIT 10 and that will give you 10 results. Oh, and my comment comment, that was "in a nutshell" ;-) Commented Jan 8, 2014 at 23:10
  • i think there may be some confusion, because i believe (correct me if I'm wrong) but MSSQL and MYSQL handle limits differently. for mysql the numbers are not a lower and upper bounds. see my answer below for how limit works. Commented Jan 8, 2014 at 23:13

1 Answer 1

3

the syntax for MYSQL LIMIT is LIMIT OFFSET COUNT, so what your second query is saying is start at record 5, and show me 10 results. does your table only have 6 records in it? since that's less than 10 it will only show those 6 available.

From the SQL manual

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

AND

With one argument, the value specifies the number of rows to return from the beginning of the result set:

Sign up to request clarification or add additional context in comments.

2 Comments

if you could click the checkbox next to the answer to accept it, that would be appreciated.
thanks a lot man I should check the google first but you realy helped me a lot ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.