Rumah  >  Artikel  >  php教程  >  PHP for SQLSrv 操作类

PHP for SQLSrv 操作类

不言
不言asal
2018-05-15 10:10:154604semak imbas

跳至 [1] [2] [全屏预览]

<?php

/**
* SqlServer操作类(sqlsrv)
* Class SQLSrv
*/
class SQLSrv
{
   private $dbhost;
   private $dbuser;
   private $dbpw;
   private $dbname;
   private $port;
   private $result;
   private $connid = 0;
   private $insertid = 0;
   private $cursor = 0;
   public static $instance = null;

   public function __construct($db)
   {
       function_exists("sqlsrv_connect") or die("<pre class="brush:php;toolbar:false">请先安装 sqlsrv 扩展。");

       $this->dbhost = !empty($db[&#39;hostname&#39;]) ? $db[&#39;hostname&#39;] : &#39;localhost&#39;;
       $this->dbuser = $db[&#39;username&#39;];
       $this->dbpw = $db[&#39;password&#39;];
       $this->dbname = $db[&#39;dbname&#39;];
       $this->port = !empty($db[&#39;port&#39;]) ? $db[&#39;port&#39;] : 1433;
       $this->connect();
   }

   public static function getdatabase($db){
       if(empty(self::$instance)){
           self::$instance = new SQLSrv($db);
       }
       return self::$instance;
   }

   /**
    * 连接数据库
    * @return int
    */
   private function connect()
   {
       $serverName = "{$this->dbhost}, {$this->port}";
       $connectionInfo = array( "Database"=>$this->dbname, "UID"=>$this->dbuser, "PWD"=>$this->dbpw);
       if(!$this->connid = @sqlsrv_connect($serverName, $connectionInfo)){
           $this->halt(print_r( sqlsrv_errors(), true));
       }

       return $this->connid;
   }

   /**
    * 执行sql
    * @param $sql
    * @return mixed
    */
   public function query($sql)
{
if(empty($sql)){
           $this->halt(&#39;SQL IS NULL!&#39;);
}

$result = sqlsrv_query($this->connid, $sql);

if(!$result){  //调试用,sql语句出错时会自动打印出来
           $this->halt(&#39;MsSQL Query Error&#39;, $sql);
}

       $this->result = $result;

return $this->result;
}

   /**
    * 获取一条数据(一维数组)
    * @param $sql
    * @return array|bool
    */
   public function find($sql)
   {
       $this->result = $this->query($sql);
       $args = $this->fetch_array($this->result);
       return $args ;
   }

   /**
    * 获取多条(二维数组)
    * @param $sql
    * @param string $keyfield
    * @return array
    */
   public function findAll($sql, $keyfield = &#39;&#39;)
   {
       $array = array();
       $this->result = $this->query($sql);
       while($r = $this->fetch_array($this->result)){
           if($keyfield){
               $key = $r[$keyfield];
               $array[$key] = $r;
           }else{
               $array[] = $this->objectToArray($r);
           }
       }
       return $array;
   }

   /**
    * 对象转数组
    * @param $obj
    * @return array
    */
   private function objectToArray($obj){
       $ret = array();
       foreach ($obj as $key => $value) {
           if (gettype($value) == "array" || gettype($value) == "object"){
               $ret[$key] =  $this->objectToArray($value);
           }else{
               $ret[$key] = $value;
           }
       }
       return $ret;
   }

   public function fetch_array($query, $type = SQLSRV_FETCH_ASSOC)
   {
       if(is_resource($query)) return sqlsrv_fetch_array($query, $type);
       if($this->cursor < count($query)){
           return $query[$this->cursor++]; 
       }
       return FALSE; 
   }

   public function affected_rows()
   {
       return sqlsrv_rows_affected($this->connid);
   }

   public function num_rows($query)
   {
       return is_array($query) ? count($query) : sqlsrv_num_rows($query);
   }

   public function num_fields($query)
   {
       return sqlsrv_num_fields($query);
   }

   /**
    * 释放连接资源
    * @param $query
    */
   public function free_result($query)
   {
       if(is_resource($query)) @sqlsrv_free_stmt($query);
   }

   public function insert_id()
   {
       return $this->insertid;
   }

   public function fetch_row($query)
   {
       return sqlsrv_num_rows($query);
   }

   /**
    * 关闭数据库连接
    * @return bool
    */
   public function close()
   {
       return sqlsrv_close($this->connid);
   }

   /**
    * 抛出错误
    * @param string $message
    * @param string $sql
    */
   public function halt($message = &#39;&#39;, $sql = &#39;&#39;)
   {
       $_sql = !empty($sql) ? "MsSQL Query:$sql <br>" : &#39;&#39;;
       exit("<pre class="brush:php;toolbar:false">{$_sql}Message:$message");
   }

   /**
    * 开始一个事务.
    */
   public function begin()
   {
       return sqlsrv_begin_transaction($this->connid);
   }

   /**
    * 提交一个事务.
    */
   public function commit()
   {
       return sqlsrv_commit($this->connid);
   }

   /**
    * 回滚一个事务.
    */
   public function rollback()
   {
       return sqlsrv_rollback($this->connid);
   }

   /**
    * 返回服务器信息
    * @return array
    */
   public static function serverInfo(){
       return sqlsrv_server_info($this->connid);
   }

   /**
    * 返回客户端信息
    * @return array|null
    */
   public static function clientInfo(){
       return sqlsrv_client_info($this->connid);
   }

   /**
    * 析构函数,关闭数据库,垃圾回收
    */
   public function __destruct()
   {
       if(!is_resource($this->connid)){
           return;
       }

       $this->free_result($this->result);
       $this->close();
   }
}

2. [代码]使用方法 跳至 [1] [2] [全屏预览]

<?php
require "database/sqlsrv_driver.php";     //导入数据库操作类

$_db = array( //数据库连接信息
   &#39;hostname&#39; => &#39;localhost&#39;, //主机地址
   &#39;username&#39; => &#39;sa&#39;, //用户名
   &#39;password&#39; => &#39;123&#39;, //密码
   &#39;dbname&#39; => &#39;test_db&#39;, //数据库名
   &#39;port&#39; => 1433, //端口  默认1433
);
$db = SQLSrv::getdatabase($_db); //数据库连接

#最终 $db 就是你的数据库实例

#查询示例
$sql = "select * from table1";

#查询单条:
$result = $db->find($sql);

#查询多条:
$result = $db->findAll($sql);

#执行增删改:
$result = $db->query($sql);

#...


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:微信开发代码Artikel seterusnya:检查代理ip的有效性