在 PHP 中使用 SQL Server達到分頁的功能
參考網址:
// http://msdn.microsoft.com/en-US/library/cc296161(v=SQL.90).aspx
// http://blogs.msdn.com/b/brian_swan/archive/2010/07/21/how-to-page-data-with-the-sql-server-drivers-for-php.aspx
<?php
/* Connect to the local server using Windows Authentication and
specify the AdventureWorks database as the database in use. */
$serverName = "sqlserver";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>"opencourse");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Set up and execute the query. */
$tsql = "select dpt_name, area_name, cur_code, cur_name, tea_name from cur_list";
$stmt = sqlsrv_query( $conn, $tsql,array(),
array( "Scrollable" => SQLSRV_CURSOR_STATIC ));
if( $stmt === false)
{
echo "Error in query preparation/execution.\n";
die( print_r( sqlsrv_errors(), true));
}
/* Retrieve each row as an associative array and display the results.*/
$i=0;
$offset = 4;
for ($i=0;$i<10;$i++) {
$rs = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE,$offset+$i);
echo $rs['dpt_name'].' '.$rs['cur_name']."<br>\n";
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>