• PHP操作MSSql


    <?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);
            }
        }
  • 相关阅读:
    人工智能深度学习:TensorFlow2.0如何保持和读取模型?
    人工智能深度学习:TensorFlow2.0实现回归问题
    InnoDB存储引擎中的锁
    Spring源码系列8
    Spring源码系列7
    Spring源码系列6
    Spring源码系列5
    Spring源码系列4
    Spring源码系列3
    Spring源码系列2
  • 原文地址:https://www.cnblogs.com/zhaobijin/p/5813318.html
Copyright © 2020-2023  润新知