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 相关文章推荐
DISCUZ 分页代码
Jan 02 PHP
PHP 表单提交给自己
Jul 24 PHP
php快速url重写更新版[需php 5.30以上]
Apr 25 PHP
php disk_free_space 返回目录可用空间
May 10 PHP
php跨域cookie共享使用方法
Feb 20 PHP
PHP中error_log()函数的使用方法
Jan 20 PHP
PHP+jQuery+Ajax实现分页效果 jPaginate插件的应用
Oct 09 PHP
PHP实现判断数组是一维、二维或几维的方法
Feb 06 PHP
浅谈Laravel队列实现原理解决问题记录
Aug 19 PHP
php生成静态页面并实现预览功能
Jun 27 PHP
Yii实现微信公众号场景二维码的方法实例
Aug 30 PHP
php远程请求CURL实例教程(爬虫、保存登录状态)
Dec 10 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网页木马一枚 附PHP木马的防范方法
2009/10/09 PHP
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
2013/07/03 PHP
解决php接收shell返回的结果中文乱码问题
2014/01/23 PHP
PHP使用ob_start生成html页面的方法
2014/11/07 PHP
php按单词截取字符串的方法
2015/04/07 PHP
浅谈PHP接入(第三方登录)QQ登录 OAuth2.0 过程中遇到的坑
2017/10/13 PHP
7款吸引人眼球的jQuery/CSS3特效实例分享
2013/04/25 Javascript
js实现无需数据库的县级以上联动行政区域下拉控件
2013/08/14 Javascript
js open() 与showModalDialog()方法使用介绍
2013/09/10 Javascript
JavaScript对象学习经验整理
2013/10/12 Javascript
jquery+CSS实现的水平布局多级网页菜单效果
2015/08/24 Javascript
js+canvas绘制矩形的方法
2016/01/28 Javascript
JS获取一个未知DIV高度的方法
2016/08/09 Javascript
Easyui在treegrid添加控件的实现方法
2017/06/23 Javascript
JavaScript基础进阶之数组方法总结(推荐)
2017/09/04 Javascript
微信小程序开发问题之wx.previewImage
2018/12/25 Javascript
layui添加动态菜单与选项卡
2019/07/26 Javascript
Vue.use()在new Vue() 之前使用的原因浅析
2019/08/26 Javascript
在weex中愉快的使用scss的方法步骤
2020/01/02 Javascript
Python中import导入上一级目录模块及循环import问题的解决
2016/06/04 Python
Python使用selenium实现网页用户名 密码 验证码自动登录功能
2018/05/16 Python
python使用rpc框架gRPC的方法
2018/08/24 Python
在django admin中添加自定义视图的例子
2019/07/26 Python
python实现广度优先搜索过程解析
2019/10/19 Python
python实现AdaBoost算法的示例
2020/10/03 Python
前端面试必备之CSS3的新特性
2017/09/05 HTML / CSS
HTML5实现多张图片上传功能
2016/03/11 HTML / CSS
html2 canvas生成清晰的图片实现打印功能
2019/09/23 HTML / CSS
荷兰本土平价百货:HEMA
2017/10/23 全球购物
介绍一下RMI的基本概念
2016/12/17 面试题
本科生自荐信
2014/06/18 职场文书
十佳党员事迹材料
2014/08/28 职场文书
个人党性分析材料
2014/12/19 职场文书
法院执行局工作总结
2015/08/11 职场文书
教师学习中国梦心得体会
2016/01/05 职场文书
小学三年级数学教学反思
2016/02/16 职场文书