使用SQL Server分页无法返回正确的结果

时间:2019-02-22 21:16:49

标签: php sql sql-server

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Pagination</title>
</head>
<body>
<?php

// define how many results you want per page
$results_per_page = 3;
// find out the number of results stored in database
$AllRows = count($sql->query("SELECT * FROM $dbs[WEB2].._WebsiteNews")->fetchAll());
// determine number of total pages available
$number_of_pages = ceil($AllRows/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['news'])) {
  $page = 1;
} else {
  $page = $_GET['news'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
// retrieve selected results from database and display them on page
$PlayersQuery = "SELECT  * FROM $dbs[WEB2].._WebsiteNews ORDER BY No OFFSET $this_page_first_result ROWS FETCH NEXT $results_per_page ROWS ONLY ;";
    $query = $sql->Query1($PlayersQuery);
while ($row = $sql->QueryFetchArray($query)) {
  echo $row['No'] . ' ' . $row['Title']. '<br>';
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
  echo '<a href="/news=' . $page . '">' . $page . '</a> ';
}
?>
</body>
</html>

我喜欢7页,我只会得到相同的结果(每页3个相同的结果);不知道这是怎么回事。

我一直在寻找使用SQL Server来实现它,但是所有人都使用MySQL。如果有人知道如何使它工作,请帮助我-谢谢

2 个答案:

答案 0 :(得分:0)

MySQL

$PlayersQuery = "SELECT  * FROM $dbs[WEB2].._WebsiteNews ORDER BY No LIMIT $this_page_first_result, $results_per_page;";

MSSQL

$PlayersQuery = "SELECT  * FROM $dbs[WEB2].._WebsiteNews ORDER BY No 
OFFSET $this_page_first_result ROWS
FETCH NEXT $results_per_page ROWS ONLY;

答案 1 :(得分:0)

在这里,我的代码得到的结果 http://prntscr.com/mouyx1此第一页 http://prntscr.com/mouzc9第二页与第一页的结果相同 而且所有页面都是这样 不知道为什么... 每页显示的首页结果相同 从4个小时开始尝试修复它^^