简体   繁体   中英

php mysql limit doesnt work

I am telling my sql script to generate this:

       $result=mysql_query("SELECT comment_id FROM comments WHERE thread_id=38 LIMIT 2,7");

I dont know why it happens, but it returns to me 7 comments (posts/results) rather than 5.

is my statment is wrong? or is it because when I refresh the page more html is added in addition to the previous html ..?

The clause Limit X, Y included at the end of the select query means X is the starting point (remember the first record is 0 ) and Y is the number records to display.

To get 5 records starting from 2nd record you have to specify: limit 2,5 .

Second argument should be count, not last element. (2,5) in your case

Use this:

$result = mysql_query("SELECT comment_id FROM comments WHERE thread_id=38 LIMIT 2,5");
// Maybe you want to use (0,5) instead!?

Examples

SELECT * FROM `your_table` LIMIT 0, 10 
// This will display the first 10 results from the database.

SELECT * FROM `your_table` LIMIT 5, 5
// This will show records 6, 7, 8, 9, and 10

By your example everything correct.

MySQL Limit first value telling from what row need to start, and the second value how much result need to return.

SELECT comment_id FROM comments WHERE thread_id=38 LIMIT 5

Will return first 5 rows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM