Home php教程 php手册 PHP和SQL Sever实现分页显示

PHP和SQL Sever实现分页显示

Jun 21, 2016 am 09:07 AM
echo gt lt mssql page

分页|显示

【摘 要】 我们用mssql_num_rows()函数得到当前查询的记录数,mssql_data_seek()函数是分页显示的关键函数

    我们在浏览网页时,经常看到分页显示的页面。如果想把大量数据提供给浏览者,分页显示是个非常实用的方法。在下面的文章中,我们将介绍如何用php和MS SQL Server实现对数据库中纪录的分页显示。

   在本例中,我们用mssql_num_rows()函数得到当前查询的记录数,结合页面大小SgPageSize,得到当前记录集要显示的页面数,为分页显示打下了基础。mssql_data_seek()函数是分页显示的关键函数,该函数的第二个参数标示当前纪录的偏移量,根据这个偏移量就可以找到要显示的页面。


  分页显示源程序:







php分页







//为了便于理解和更快地应用到工作中去,我们以MS SQL Server的NorthWind数据库Customers表为例。

$gPageSize= 10; //每页显示的记录数

$hostname = "localhost"; //MSSQL Server

$dbuser = "sa"; //用户名

$dbpasswd = "1111111"; //密码

//连接数据库

$id = mssql_connect($hostname,$dbuser,$dbpasswd) or die("无法连接数据库服务器!");

//选择数据库,为了方便,这里以MSSQL Server的NorthWind数据库为例

$db = mssql_select_db("northwind",$id) or die("无法连接数据库!");

//以Customers表为例,构建查询字符串

$query = "select * from Customers";

//执行查询语句

$rresult = mssql_query($query) or die("无法执行SQL:$query");

//$page变量标示当前显示的页

if(!isset($page)) $page=1;

if($page==0) $page=1;

//得到当前查询到的纪录数 $nNumRows

if(($nNumRows= mssql_num_rows($rresult))
{

echo "

没有纪录";

exit;

};

//得到最大页码数MaxPage

$MaxPage = (int)ceil($nNumRows/$gPageSize);

if((int)$page > $MaxPage)

$page=$maxPage;

?>

echo "
$page 页,共 $MaxPage 页
";?>








//显示表格头

for($iCnt = 0; $iCnt
{

echo "" ;

}

?>





//根据偏移量($page - 1)*$gPageSize,运用mssql_data_seek函数得到要显示的页面

if( mssql_data_seek($rresult,($page-1)*$gPageSize) )

{

$i=0;

//循环显示当前纪录集

for($i;$i
{

echo "";

//得到当前纪录,填充到数组$arr;

$arr= mssql_fetch_row($rresult);

if($arr)

{

//循环显示当前纪录的所有字段值

for($nOffSet = 0;$nOffSet
{

echo "";

}

}

echo "";

}

}

?>

".mssql_field_name($rresult,$iCnt)."
".$arr[$nOffSet]."












//首页和上一页的链接

if( $nNumRows>1 && $page>1)

{

$prevPage=$page-1;

echo " 首页 ";

echo " 上一页 ";

}

//下一页和末页的链接

if( $page>=1 && $page
{

$nextPage= $page+1;

echo " 下一页 ";

echo " 末页 ";

}

?>







  
把上述脚本程序进行一些修改,你就可以建立自己的服务器端分页显示的脚本程序了。



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to connect php to mssql database How to connect php to mssql database Oct 23, 2023 pm 12:02 PM

Methods for php to connect to mssql database include using PHP's MSSQL extension, using PDO, etc. Detailed introduction: 1. Use PHP's MSSQL extension method to ensure that PHP has the MSSQL extension installed. You can check whether the mssql extension is enabled in the PHP configuration file (php.ini); 2. Use the PDO method to ensure that PHP has the PDO extension installed. You can check whether the pdo_sqlsrv extension is enabled in the PHP configuration file (php.ini).

Detailed guide to install PHP and configure MSSQL connection on Ubuntu Detailed guide to install PHP and configure MSSQL connection on Ubuntu Feb 29, 2024 am 11:15 AM

Ubuntu is a popular open source operating system commonly used to run servers. Installing PHP and configuring MSSQL connections on Ubuntu is one of the operations that many developers and system administrators often need to do. This article will provide readers with a detailed guide, including the steps to install PHP, set up Apache, install MSSQLServer, etc., and attach specific code examples. Step 1: Install PHP and related extensions First, we need to install PHP and related extensions to support PHP connections

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Detailed steps to install PHP to support MSSQL database in Ubuntu environment Detailed steps to install PHP to support MSSQL database in Ubuntu environment Feb 29, 2024 am 10:39 AM

Detailed steps for installing PHP to support MSSQL database in Ubuntu environment. When developing web applications, you often encounter situations where you need to connect to the Microsoft SQL Server (MSSQL) database. In the Ubuntu environment, to connect PHP to the MSSQL database, you need to install relevant software and configure appropriate settings. Next, we will introduce in detail the steps to install PHP to support MSSQL database in Ubuntu environment and provide specific code.

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

See all articles