Skip to content

Instantly share code, notes, and snippets.

@j796160836
Created October 3, 2013 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j796160836/6806350 to your computer and use it in GitHub Desktop.
Save j796160836/6806350 to your computer and use it in GitHub Desktop.
PHP連接 SQL Server實作分頁
$pageSize=20;
$page=@$_GET['page'];
if(strcmp($page, '')==0)
$page=1;
// 查找資料總數
$sql="SELECT TOP 1 count(*) as count FROM testtable";
$stmt = j_sqlquery($sql, array());
$total=sqlsrv_num_rows($stmt);
if($row = j_fetch_array($stmt))
{
$count=$row['count'];
$pages=ceil($count / $pageSize);
echo 'count='.$count.'<br>';
echo 'pages='.$pages.'<br>';
}
if($page<=1)
$page=1;
if($page>$pages)
$page=$pages;
$startRow=($page-1)*$pageSize+1;
$sql="SELECT TOP ".$pageSize." * FROM (SELECT *,(ROW_NUMBER() OVER (ORDER BY id ASC)) AS R FROM testtable) AS A WHERE R>=".$startRow;
$stmt = j_sqlquery($sql, array());
while($row = j_fetch_array($stmt))
{
echo $row['name'].'<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment