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 相关文章推荐
使用apache模块rewrite_module (转)
Feb 14 PHP
Ajax实时验证用户名/邮箱等是否已经存在的代码打包
Dec 01 PHP
php ctype函数中文翻译和示例
Mar 21 PHP
yii2.0之GridView自定义按钮和链接用法
Dec 15 PHP
用php守护另一个php进程的例子
Feb 13 PHP
php基于GD库画五星红旗的方法
Feb 24 PHP
Smarty最简单实现列表奇偶变色的方法
Jul 01 PHP
thinkphp在php7环境下提示Cannot use ‘String’ as class name as it is reserved的解决方法
Sep 30 PHP
php基于自定义函数记录log日志方法
Jul 21 PHP
PHP简单实现模拟登陆功能示例
Sep 15 PHP
PHP实现链式操作的三种方法详解
Nov 16 PHP
PHP以json或xml格式返回请求数据的方法
May 31 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 APC的安装与使用详解
2013/06/13 PHP
php防止SQL注入详解及防范
2013/11/12 PHP
ThinkPHP实现更新数据实例详解(demo)
2016/06/29 PHP
laravel 获取某个查询的查询SQL语句方法
2019/10/12 PHP
通过PHP实现用户注册后邮箱验证激活
2020/11/10 PHP
jQuery移动和复制dom节点实用DOM操作案例
2012/12/17 Javascript
Extjs中通过Tree加载右侧TabPanel具体实现
2013/05/05 Javascript
GRID拖拽行的实例代码
2013/07/18 Javascript
JavaScript获取图片的原始尺寸以宽度为例
2014/05/04 Javascript
node.js操作mongoDB数据库示例分享
2014/11/26 Javascript
详解Javascript继承的实现
2016/03/25 Javascript
JS阻止事件冒泡行为和闭包的方法
2016/06/16 Javascript
jQuery对checkbox 复选框的全选全不选反选的操作
2016/08/09 Javascript
js 文字超出长度用省略号代替,鼠标悬停并以悬浮框显示实例
2016/12/06 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
Thinkjs3新手入门之如何使用静态资源目录
2017/12/06 Javascript
d3.js实现自定义多y轴折线图的示例代码
2018/05/30 Javascript
JS监听滚动和id自动定位滚动
2018/12/18 Javascript
javascript将扁平的数据转为树形结构的高效率算法
2020/02/27 Javascript
JavaScript cookie原理及使用实例
2020/05/08 Javascript
JavaScript手写数组的常用函数总结
2020/11/22 Javascript
在Linux系统上部署Apache+Python+Django+MySQL环境
2015/12/24 Python
python动态加载包的方法小结
2016/04/18 Python
python实现BackPropagation算法
2017/12/14 Python
python 一个figure上显示多个图像的实例
2019/07/08 Python
利用Python脚本实现自动刷网课
2020/02/03 Python
手把手教你安装Windows版本的Tensorflow
2020/03/26 Python
详解Python3 定义一个跨越多行的字符串的多种方法
2020/09/06 Python
详解CSS3实现响应式手风琴效果
2020/06/10 HTML / CSS
C#中类(class)与结构(struct)的异同
2013/11/03 面试题
仓库主管岗位职责
2014/03/02 职场文书
中秋节国旗下演讲稿
2014/09/05 职场文书
天下第一关导游词
2015/02/06 职场文书
行政助理岗位职责范本
2015/04/11 职场文书
2015年重阳节主持词
2015/07/04 职场文书
Win11绿屏怎么办?Win11绿屏死机的解决方法
2021/11/21 数码科技