php的mssql数据库连接类实例


Posted in PHP onNovember 28, 2014

本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。

具体实现代码如下:

class DB_Sql { 

  var $Host     = ""; 

  var $Database = ""; 

  var $User     = ""; 

  var $Password = ""; 

  var $Link_ID  = 0; 

  var $Query_ID = 0; 

  var $Record   = array(); 

  var $Row      = 0; 

   

  var $Errno    = 0; 

  var $Error    = ""; 

  var $Auto_Free = 0;     ## set this to 1 to automatically free results 

   

  function DB_Sql($query = "") { 

      $this->query($query); 

  } 

  function connect() { 

    if ( 0 == $this->Link_ID ) { 

      $this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password); 

      if (!$this->Link_ID) 

        $this->halt("Link-ID == false, mssql_pconnect failed"); 

      else 

          @mssql_select_db($this->Database, $this->Link_ID); 

    } 

  } 

  function free_result(){ 

      mssql_free_result($this->Query_ID); 

      $this->Query_ID = 0; 

  } 

   

  function query($Query_String)  

  { 

     

    /* No empty queries, please, since PHP4 chokes on them. */ 

    if ($Query_String == "") 

      /* The empty query string is passed on from the constructor, 

       * when calling the class without a query, e.g. in situations 

       * like these: '$db = new DB_Sql_Subclass;' 

       */ 

      return 0; 

      if (!$this->Link_ID) 

        $this->connect(); 

     

#   printf("<br>Debug: query = %s<br> ", $Query_String); 

 

 $this->Query_ID = mssql_query($Query_String, $this->Link_ID); 

    $this->Row = 0; 

    if (!$this->Query_ID) { 

      $this->Errno = 1; 

      $this->Error = "General Error (The MSSQL interface cannot return detailed error messages)."; 

      $this->halt("Invalid SQL: ".$Query_String); 

    } 

    return $this->Query_ID; 

  } 

   

  function next_record() { 

       

    if ($this->Record = mssql_fetch_row($this->Query_ID)) { 

      // add to Record[<key>] 

      $count = mssql_num_fields($this->Query_ID); 

      for ($i=0; $i<$count; $i++){ 

          $fieldinfo = mssql_fetch_field($this->Query_ID,$i); 

        $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i]; 

      } 

      $this->Row += 1; 

      $stat = 1; 

    } else { 

      if ($this->Auto_Free) { 

            $this->free_result(); 

          } 

      $stat = 0; 

    } 

    return $stat; 

  } 

   

  function seek($pos) { 

        mssql_data_seek($this->Query_ID,$pos); 

      $this->Row = $pos; 

  } 

  function metadata($table) { 

    $count = 0; 

    $id    = 0; 

    $res   = array(); 

    $this->connect(); 

    $id = mssql_query("select * from $table", $this->Link_ID); 

    if (!$id) { 

      $this->Errno = 1; 

      $this->Error = "General Error (The MSSQL interface cannot return detailed error messages)."; 

      $this->halt("Metadata query failed."); 

    } 

    $count = mssql_num_fields($id); 

     

    for ($i=0; $i<$count; $i++) { 

        $info = mssql_fetch_field($id, $i); 

      $res[$i]["table"] = $table; 

      $res[$i]["name"]  = $info["name"]; 

      $res[$i]["len"]   = $info["max_length"]; 

      $res[$i]["flags"] = $info["numeric"]; 

    } 

    $this->free_result(); 

    return $res; 

  } 

   

  function affected_rows() { 

// Not a supported function in PHP3/4.  Chris Johnson, 16May2001. 

//    return mssql_affected_rows($this->Query_ID); 

    $rsRows = mssql_query("Select @@rowcount as rows", $this->Link_ID); 

    if ($rsRows) {        

       return mssql_result($rsRows, 0, "rows"); 

    } 

  } 

   

  function num_rows() { 

    return mssql_num_rows($this->Query_ID); 

  } 

   

  function num_fields() { 

    return mssql_num_fields($this->Query_ID); 

  } 

  function nf() { 

    return $this->num_rows(); 

  } 

   

  function np() { 

    print $this->num_rows(); 

  } 

   

  function f($Field_Name) { 

    return $this->Record[strtolower($Field_Name)]; 

  }

   

  function p($Field_Name) { 

    print $this->f($Field_Name); 

  } 

   

  function halt($msg) { 

    printf("</td></tr></table><b>Database error:</b> %s<br> ", $msg); 

    printf("<b>MSSQL Error</b>: %s (%s)<br> ", 

      $this->Errno, 

      $this->Error); 

    die("Session halted."); 

  } 

}

希望本文所述对大家的PHP程序设计有所帮助。

PHP 相关文章推荐
提升PHP执行速度全攻略(上)
Oct 09 PHP
smarty模板嵌套之include与fetch性能测试
Dec 05 PHP
php流量统计功能的实现代码
Sep 29 PHP
域名和cookie问题(域名后缀)
Oct 10 PHP
用PHP编写和读取XML的几种方式
Jan 12 PHP
深入PHP数据缓存的使用说明
May 10 PHP
php 启动时报错的简单解决方法
Jan 27 PHP
php接口和抽象类使用示例详解
Mar 02 PHP
php上传大文件失败的原因及应对策略
Oct 20 PHP
如何离线执行php任务
Feb 21 PHP
php redis实现文章发布系统(用户投票系统)
Mar 04 PHP
PHP+百度AI OCR文字识别实现了图片的文字识别功能
May 08 PHP
smarty中post用法实例
Nov 28 #PHP
smarty简单入门实例
Nov 28 #PHP
php最简单的删除目录与文件实现方法
Nov 28 #PHP
php查找指定目录下指定大小文件的方法
Nov 28 #PHP
thinkphp四种url访问方式详解
Nov 28 #PHP
thinkphp数据查询和遍历数组实例
Nov 28 #PHP
php中fgetcsv()函数用法实例
Nov 28 #PHP
You might like
php读取mysql乱码,用set names XXX解决的原理分享
2011/12/29 PHP
php文件夹与文件目录操作函数介绍
2013/09/09 PHP
php中fgetcsv()函数用法实例
2014/11/28 PHP
PHP文件缓存类示例分享
2015/01/30 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
2015/06/19 PHP
33道php常见面试题及答案
2015/07/06 PHP
php算法实例分享
2015/07/14 PHP
php转换上传word文件为PDF的方法【基于COM组件】
2019/06/10 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
新浪刚打开页面出来的全屏广告代码
2007/04/02 Javascript
javascript新手语法小结
2008/06/15 Javascript
Javascript中匿名函数的多种调用方式总结
2013/12/06 Javascript
js数字转换为float,取N位小数
2014/02/08 Javascript
document.compatMode的CSS1compat使用介绍
2014/04/03 Javascript
javascript常见数字进制转换实例分析
2016/04/21 Javascript
AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
2016/11/02 Javascript
bootstrap laydate日期组件使用详解
2017/01/04 Javascript
微信小程序 chooseImage选择图片或者拍照
2017/04/07 Javascript
在vue中使用vuex,修改state的值示例
2019/11/08 Javascript
vue css 引入asstes中的图片无法显示的四种解决方法
2020/03/16 Javascript
JavaScript实现放大镜效果代码示例
2020/04/29 Javascript
Python Property属性的2种用法
2015/06/21 Python
Python实现的人工神经网络算法示例【基于反向传播算法】
2017/11/11 Python
Django框架HttpResponse对象用法实例分析
2019/11/01 Python
Python3标准库之functools管理函数的工具详解
2020/02/27 Python
基于Python数据分析之pandas统计分析
2020/03/03 Python
python中watchdog文件监控与检测上传功能
2020/10/30 Python
深入了解canvas在移动端绘制模糊的问题解决
2019/04/30 HTML / CSS
史泰博(Staples)中国官方网站:办公用品一站式采购
2016/09/05 全球购物
英国的屈臣氏:Boots博姿
2017/12/23 全球购物
JD Sports西班牙:英国领先的运动服装公司
2020/01/06 全球购物
计算机应用专业自荐信
2014/07/05 职场文书
2014年“世界无车日”活动方案
2014/09/21 职场文书
群众路线党员自我评议范文2014
2014/09/24 职场文书
详解Mysq MVCC多版本的并发控制
2022/04/29 MySQL
vue如何在data中引入图片的正确路径
2022/06/05 Vue.js