Created
October 3, 2013 07:29
-
-
Save j796160836/6806350 to your computer and use it in GitHub Desktop.
PHP連接 SQL Server實作分頁
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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