<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /*****PHP操作SqlServer数据库********/ class Sqlsrv { private $hostname="JKT-001SQLEXPRESS"; private $username="sa"; private $password="52ebook"; private $dbname="enterprise"; private $conn; function __construct() { try{ $constr=array("Database"=>$this->dbname,"CharacterSet" => "UTF-8","UID"=>$this->username,"PWD"=>$this->password); $this->conn=sqlsrv_connect($this->hostname,$constr); if($this->conn==false) { print_r(sqlsrv_errors(),true); die; } }catch(Exception $ex) { echo $ex->getMessage(); } } /*查询所有数据*/ public function QuaryAll($sql) { try{ $rs=sqlsrv_query($this->conn,$sql); $result=array(); while($row = sqlsrv_fetch_array($rs,SQLSRV_FETCH_NUMERIC)) { $result[]=$row; } sqlsrv_free_stmt($rs); return $result; }catch(Exception $ex) { echo $ex->getMessage(); } } /*执行sql语句,返回所影响的行数*/ public function AffectedRows($sql) { try{ $rs=sqlsrv_query($this->conn,$sql); $result=sqlsrv_rows_affected($rs); sqlsrv_free_stmt($rs); return $result; }catch(Exception $ex) { echo $ex->getMessage(); } } /** 实现分页查询* *@tablename 表名 *@columns 字段列表 *@pagenum 当前页码 *@pagecount 页面条数 *@prikey 分页主键 *@orderkey 排序主键 */ public function QueryPage($tablename,$columns='*',$pagenum=20,$pagecount=1,$prikey,$orderkey) { try{ $columnnames='*'; if(is_array($columns)) { $columnnames=implode(',',$columns); } $sql='select top '.$pagenum.' '.$columnnames.' from '.$tablename.' where '.$prikey.' not in (select top '.$pagenum*($pagecount-1).' '.$prikey.' from '.$tablename.' order by '.$orderkey.') order by '.$orderkey; $rs=sqlsrv_query($this->conn,$sql); $result=array(); while($row = sqlsrv_fetch_array($rs,SQLSRV_FETCH_NUMERIC)) { $result[]=$row; } sqlsrv_free_stmt($rs); return $result; }catch(Exception $ex) { echo $ex->getMessage(); } } function __destruct() { sqlsrv_close($this->conn); } }