简体   繁体   中英

php bind_param for limit

I have problem with binding values to mysql query in php.

$this->conn->prepare("SELECT * FROM tablename LIMIT ? , ? ");
$pageStart = 11; 
$pageEnd = 20 ;
$stmt->bind_param("ii" , $pageStart , $pageEnd );
$stmt->execute();

This is returning 20 rows. Any help?

This is correct behaviour.

From the documentation :

...
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
...

the number after the comma is rowcount.

If you want 10 records, just tell that:

SELECT * FROM tablename LIMIT 11 , 10;

Query is executing correctly. Your parameters having incorrect values as per expected output.

If you want records from 11-20 query should be:

SELECT * FROM tablename LIMIT 11 , 10

as you are passing 20 as second parameter its returning 20 rows

You can use LIMIT offset, row_ count or LIMIT row_count OFFSET offset

See documentation http://dev.mysql.com/doc/refman/5.0/en/select.html

$pageStart = 11; Means the result start for 11 and $pageEnd = 20 ; means 20 result after 11

So you get 20 result now.

To get result up to 10 you need to set $pageEnd = 10. It will return you result 11 to 21

Regarding the names of your variables you should change

$stmt->bind_param("ii" , $pageStart , $pageEnd );

to

$stmt->bind_param("ii" , $pageStart , $pageEnd - $pageStart + 1);

because the second parameter of LIMIT is the amount of rows to be selected. The first parameter is the offset.

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