PHP MSSQL 分页实例

本文实例为大家分享了PHP MSSQL可刷新分页实例,具体内容如下:

  1. <?php
  2. /*
  3. '页面说明:
  4. */
  5. $link=mssql_connect("MYSQL2005","sa","123456") or die("db link error!".mssql_error());
  6. mssql_select_db("edu_dzk",$link) or die("db open error!".mssql_error());
  7. //mssql_query("set names 'utf8'");
  8. $sqlstrcount="SELECT count(1) FROM reguser";
  9. $page = $_GET["page"];
  10. if(!is_numeric($page)){
  11. $page="1";
  12. }
  13. $pagesize="30";
  14. //$sql=mysql_query($sqlstr);
  15. //$totalnum=mysql_num_rows($sql);
  16. $sql=mssql_query($sqlstrcount);
  17. list($totalnum)=mssql_fetch_row($sql);
  18. $pagecount=ceil($totalnum/$pagesize);
  19. $offset=($page-1)*$pagesize;
  20. $sqlstr="Select top ".($pagesize)." * from reguser where id not in (select top ".(($page-1)*$pagesize)." id from reguser order by id desc) order by id desc";
  21. $sql=mssql_query($sqlstr);
  22. $result="";
  23. while($result=mssql_fetch_array($sql)){
  24. echo $result["id"].'<br>';
  25. }
  26. showpageinfo2($totalnum,$pagecount,$page,"&z=".$z);
  27. mssql_Close($link);
  28. function showpageinfo2($totalnum,$pagecount,$page,$filster)
  29. {
  30. echo "共计: $totalnum 条记录,共 $pagecount 页,当前是第 $page 页 ";
  31. if($page!=1)
  32. {
  33. echo "<a href="?page=1$filster">首页</a> ";
  34. echo "<a href="?page=".($page-1)."$filster">上一页</a> ";
  35. }
  36. if($page<$pagecount)
  37. {
  38. echo "<a href="?page=".($page+1)."$filster">下一页</a> ";
  39. echo "<a href="?page=$pagecount$filster">尾页</a> ";
  40. }
  41. //phpfensi.com
  42. }
  43. ?>

下面为大家分享了PHP+MSSQL通用分页公式,供大家参考,具体内容如下:

  1. <?php
  2. //phpinfo();
  3. //$conn=mssql_connect('192.168.0.3','app','web')or die (mssql_get_last_message());
  4. //if($conn)
  5. //{
  6. //?echo "success";
  7. //}else{
  8. //?echo "fail";
  9. //}
  10. ?>
  11. <title>PHP分页</title>
  12. <?
  13. $gPageSize= 10; //每页显示的记录数
  14. $hostname = "."; //MsSQL Server
  15. $dbuser = "sa"; //用户名
  16. $dbpasswd = ""; //密码
  17. //连接数据库
  18. $id = mssql_connect($hostname,$dbuser,$dbpasswd) or die("无法连接数据库服务器!");
  19. $db = mssql_select_db("phmis04",$id) or die("无法连接数据库!");
  20. //以xxx表为例,构建查询字符串
  21. $query = "select top 20 ag_mate,ag_color,ag_width,ag_mode,mi_name,convert(char,ag_date,23)as ag_date,ag_grid,au_name,ag_num from acco_grid join mate_info on ag_mate=mi_id join appl_unit on au_;
  22. //执行查询语句
  23. $rresult = mssql_query($query) or die("无法执行SQL:$query");
  24. //$page变量标示当前显示的页
  25. if(!isset($page)) $page=1;
  26. if($page==0) $page=1;
  27. //得到当前查询到的纪录数 $nNumRows
  28. if(($nNumRows= mssql_num_rows($rresult))<=0)
  29. {
  30. echo "<p align="center">没有纪录";
  31. exit;
  32. };
  33. //得到最大页码数MaxPage
  34. $MaxPage = (int)ceil($nNumRows/$gPageSize);
  35. if((int)$page > $MaxPage)
  36. $page=$maxPage;
  37. ?>
  38. </p><table align="center" width="80%" > <tbody><tr><td> <? echo "<font size="2">第
  39. $page 页,共 $MaxPage 页</font>";?> </td><td></td></tr></tbody></table>
  40. <?
  41. //显示表格头
  42. for($iCnt = 0; $iCnt < mssql_num_fields($rresult); $iCnt++)
  43. {
  44. echo "" ;
  45. }
  46. ?>
  47. <?
  48. //根据偏移量($page - 1)*$gPageSize,运用mssql_data_seek函数得到要显示的页面
  49. if( mssql_data_seek($rresult,($page-1)*$gPageSize) )
  50. {
  51. $i=0;
  52. //循环显示当前纪录集
  53. for($i;$i<$gPageSize;$i++)
  54. {
  55. echo "";
  56. //得到当前纪录,填充到数组$arr;
  57. $arr= mssql_fetch_row($rresult);
  58. if($arr)
  59. {
  60. //循环显示当前纪录的所有字段值
  61. for($nOffSet = 0;$nOffSet < count($arr);$nOffSet++)
  62. {
  63. echo "";
  64. }
  65. }
  66. echo "";
  67. }
  68. }
  69. ?>
  70. <table align="center" width="80%" cellspacing="0" cellpadding="4" bordercolorlight="#CC9966" bgcolor="#00F2EE" bordercolordark="#FFFFFF" class="LZH">
  71. <tbody><tr bgcolor="#F7F2ff" ><td>".mssql_field_name($rresult,$iCnt)."</td></tr><tr font-size:12px\""><td>".$arr[$nOffSet]."</td></tr></tbody></table>
  72. <br>
  73. <hr size="1" width="80%">
  74. <div align="center" >
  75. <?
  76. //首页和上一页的链接
  77. if( $nNumRows>1 && $page>1)
  78. {
  79. $prevPage=$page-1;
  80. echo " <a href=".$PHP_SELF." ?page="1">首页</a> ";
  81. echo " <a href=".$PHP_SELF." ?page=".$prevPage.">上一页</a> ";
  82. }
  83. //下一页和末页的链接
  84. if( $page>=1 && $page<$MaxPage)
  85. {
  86. $nextPage= $page+1;
  87. echo " <a href=".$PHP_SELF." ?page=".$nextPage.">下一页</a> ";
  88. echo " <a href=".$PHP_SELF." ?page=".$MaxPage.">末页</a> ";
  89. }
  90. ?>
  91. </div>