PHP+sqlsrv的分页实现
<?php
//A表 分页,总记录,输出
/////////////////////每一页的数据数量
$messagecount= intval($_GET['messagecount']);
//////////////////要搜索第几页的数据
$currentpageindex= intval($_GET['currentpageindex']);
class Emp_ {
public $ID="";
public $UpdateTime="";
public $PageNumber="";
}
$sql="SELECT TOP
( ? ) *,
( SELECT COUNT ( ID ) FROM A WHERE B = '321' ) AS COUNT
FROM
A
WHERE
B = '321'
AND ID NOT IN (
SELECT TOP
( (?) * (?) ) ID
FROM
A
WHERE
B = '321')";
$params = array($messagecount,$messagecount,$currentpageindex);
$result= sqlsrv_query($conn,$sql,$params);
if(!$result)
{
echo "Error in executing statement 1.\n";
$arr = array('Result' => 0);
echo json_encode($arr);
die( print_r( sqlsrv_errors(), true));
}
if(sqlsrv_rows_affected($result)==0){
$arr = array('result' => '0');
echo json_encode($arr);
//
sqlsrv_close($conn);
die( print_r( sqlsrv_errors(), true));
}
else{
$datalist=array();
while($row = sqlsrv_fetch_array($result))
{
$e = new Emp_ ();
$e->ID = iconv("GB2312//IGNORE", "UTF-8", $row["ID"]) ;
$e->UpdateTime=get_object_vars($row["UpdateTime"])["date"];
$e->PageNumber=ceil($e->COUNT/$messagecount) ;
array_push($datalist,$e);
//$data= json_encode($e,JSON_UNESCAPED_UNICODE);
// echo $data;
}
echo json_encode(array('result' => 'ok','information'=>$datalist),JSON_UNESCAPED_UNICODE);
}
sqlsrv_free_stmt($result);
sqlsrv_close($conn);
?>