user3175393
user3175393

Reputation: 109

LIMIT in php sql not working

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.

Upvotes: 0

Views: 1328

Answers (1)

Andrew Brown
Andrew Brown

Reputation: 5424

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:

Upvotes: 3

Related Questions