php pdo查询sqlserver,php pdo sqlserver分页sql的处理

今日弄了点小东西,需要做一个sqlsrv的分页函数

最后也没弄成,算了,干脆有些东西写死了...

pdo_exequery为自己弄的一个pdo的查询封装函数

主要是基于这个处理:

preg_replace("/(t|T)(o|O)(p|P])\s\d{1,12}/","","select top 100 *");

首先要接收pageid及pagesize

if(isset($_GET["pageid"]))$intCurrentPage = $_GET["pageid"];

else

$intCurrentPage = 1;

$intPageSize = 20;

然后

设置一个分页时一个基准的orderid

$orderbyid="FItemID";

然后构建一个SQL用于获得总行数且包含分页的处理

$curcuror=$intCurrentPage*$intPageSize;

$SQL="select top $curcuror a.FNumber,a.FItemID,a.FName,a.FModel,d.fname as measureunits,e.fname as secmeasureunits from ".$tablename." a left join t_MeasureUnit d on a.FUnitID=d.FMeasureUnitID left join t_MeasureUnit e on a.FSecUnitID=e.FMeasureUnitID where 1=1 ";

然后进行替换,查询总行数

$newSQL=preg_replace("/(t|T)(o|O)(p|P])\s\d{1,12}/","",$SQL);

$ROW=pdo_exequery($pdo_msconnection,$newSQL);

$inumber=0;

if($ROW)$inumber=count($ROW);

最后是进行SQL的处理

if($intCurrentPage ==null)$intCurrentPage=1;

$intRecordTotal = $inumber;

if($intCurrentPage!="all"){

$intPageCount = intval(($intRecordTotal-1) / $intPageSize)+1;

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

if ($intCurrentPage > $intPageCount) {$intCurrentPage = $intPageCount;}

$intStart = $intPageSize * ($intCurrentPage - 1);

$offset=$intPageSize*($intCurrentPage-1);

if ($offset<0){$offset=0;}

//主要在于这里,进行了分页的SQL合并

$SQL="select * from (select top $intPageSize * from ($SQL)o1 order by $orderbyid desc)o2 order by $orderbyid asc";

}

else

{

$intCurrentPage=1;

$intPageCount=1;

}

$ROW=pdo_exequery($pdo_msconnection,$SQL);

$inumber=0;

if($ROW)$inumber=count($ROW);

此处即可进行数据的处理了

缺陷在于只能进行一个order by的处理,如果有其他需要,依样画葫芦,应该是没问题的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值