简体   繁体   中英

php pagination using sql server Limit and Offet

I have a problem with php pagination. I trying make a list from a db and generate pages. Below there is a part of code. If I am right I can't use limit and offset in my sql server?

$stmt = $conn->prepare('SELECT * FROM db_name ORDER BY use_by_date LIMIT :limit OFFSET :offset');

$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();


    if ($stmt->rowCount() > 0) {

        $stmt->setFetchMode(PDO::FETCH_ASSOC);
        $iterator = new IteratorIterator($stmt);
        foreach ($iterator as $row) {
            echo '<p>', $row['name'], '</p>';
        }

    }

 else {
        echo '<p>No results could be displayed.</p>';
    }

what is wrong?

It seems like you're forgetting to actually fetch the data:

$rows = $stmt->fetchAll();
foreach ($rows as $row) { ... }

http://php.net/manual/en/pdostatement.fetchall.php

Also, per sevnlabs' answer, there's no LIMIT in T-SQL.

There's no LIMIT in SQL Server. You can use SELECT TOP XY for limiting your result. The offset could be realised via the last received primary key. Something like "WHERE id > lastReturnedId" for the next page.

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