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 生成随机验证码图片代码
Feb 08 PHP
获取用户Ip地址通用方法与常见安全隐患(HTTP_X_FORWARDED_FOR)
Jun 01 PHP
php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍
Jun 18 PHP
CentOS 6.3下安装PHP xcache扩展模块笔记
Sep 10 PHP
php实现编辑和保存文件的方法
Jul 20 PHP
基础的WordPress插件制作教程
Nov 24 PHP
PHP中each与list用法分析
Jan 08 PHP
PHP中类属性与类静态变量的访问方法示例
Jul 13 PHP
PHP GD库相关图像生成和处理函数小结
Sep 30 PHP
Laravel Memcached缓存驱动的配置与应用方法分析
Oct 08 PHP
PHP操作Postgresql封装类与应用完整实例
Apr 24 PHP
在Ubuntu 18.04上安装PHP 7.3 7.2和7.0的方法
Apr 09 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函数
2006/10/09 PHP
分享PHP-pcntl 实现多进程代码
2016/09/30 PHP
php图片合成方法(多张图片合成一张)
2017/11/25 PHP
文字幻灯片
2006/06/26 Javascript
再谈javascript图片预加载技术(详细演示)
2011/03/12 Javascript
文本框获得焦点和失去焦点的判断代码
2012/03/18 Javascript
解析John Resig Simple JavaScript Inheritance代码
2012/12/03 Javascript
JavaScript控制图片加载完成后调用回调函数的方法
2015/03/20 Javascript
详解JavaScript正则表达式之RegExp对象
2015/12/13 Javascript
使用struts2+Ajax+jquery验证用户名是否已被注册
2016/03/22 Javascript
Javascript中click与blur事件的顺序详析
2017/04/25 Javascript
php 修改密码实现代码
2017/05/24 Javascript
JS中的模糊查询功能
2019/12/08 Javascript
vuex+axios+element-ui实现页面请求loading操作示例
2020/02/02 Javascript
Python实现从订阅源下载图片的方法
2015/03/11 Python
Python日志模块logging简介
2015/04/13 Python
python+opencv轮廓检测代码解析
2018/01/05 Python
Sublime开发python程序的示例代码
2018/01/24 Python
Python安装lz4-0.10.1遇到的坑
2018/05/20 Python
Numpy数据类型转换astype,dtype的方法
2018/06/09 Python
tensorflow实现加载mnist数据集
2018/09/08 Python
Django框架模板语言实例小结【变量,标签,过滤器,继承,html转义】
2019/05/23 Python
Python Opencv提取图片中某种颜色组成的图形的方法
2019/09/19 Python
Python之Class&amp;Object用法详解
2019/12/25 Python
Ted Baker英国官网:男士和女士服装及配件
2017/03/13 全球购物
自然健康的概念:Natural Healthy Concepts
2020/01/26 全球购物
iHerb俄罗斯:维生素、补品和天然产品
2020/07/09 全球购物
珠宝店促销方案
2014/03/21 职场文书
学生安全责任书范本
2014/07/24 职场文书
2014公安机关纪律作风整顿思想汇报
2014/09/13 职场文书
招标授权委托书样本
2014/09/23 职场文书
教师聘用意向书
2015/05/11 职场文书
新闻通讯稿模板
2015/07/22 职场文书
2015年“我们的节日·中秋节”活动总结
2015/07/30 职场文书
2016年“抗战胜利纪念日”71周年校园广播稿
2015/12/18 职场文书
python3实现Dijkstra算法最短路径的实现
2021/05/12 Python