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+dbfile开发小型留言本
Oct 09 PHP
修改Zend引擎实现PHP源码加密的原理及实践
Apr 14 PHP
php 常用类整理
Dec 23 PHP
php学习笔记 面向对象的构造与析构方法
Jun 13 PHP
Smarty局部缓存的几种方法简介
Jun 17 PHP
Laravel框架数据库CURD操作、连贯操作总结
Sep 03 PHP
PHP生成唯一订单号
Jul 05 PHP
浅谈php的优缺点
Jul 14 PHP
PHP中的print_r 与 var_dump 输出数组
Jun 13 PHP
Yii净化器CHtmlPurifier用法示例(过滤不良代码)
Jul 15 PHP
PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
Sep 11 PHP
laravel框架使用极光推送消息操作示例
Feb 15 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
使用pthreads实现真正的PHP多线程(需PHP5.3以上版本)
2014/05/05 PHP
四种php中webservice实现的简单架构方法及实例
2015/02/03 PHP
部署PHP时的4个配置修改说明
2015/10/19 PHP
/etc/php-fpm.d/www.conf 配置注意事项
2017/02/04 PHP
PHP使用mongoclient简单操作mongodb数据库示例
2019/02/08 PHP
PHP超全局变量实现原理及代码解析
2020/09/01 PHP
addRule在firefox下的兼容写法
2006/11/30 Javascript
提升你网站水平的jQuery插件集合推荐
2011/04/19 Javascript
JavaScript计划任务后台运行的方法
2015/12/18 Javascript
基于javascript实现根据身份证号码识别性别和年龄
2016/01/22 Javascript
jQuery简单获取键盘事件的方法
2016/01/22 Javascript
Vuejs第八篇之Vuejs组件的定义实例解析
2016/09/05 Javascript
简单的js计算器实现
2016/10/26 Javascript
微信小程序开发animation心跳动画效果
2017/08/16 Javascript
webpack4.x CommonJS模块化浅析
2018/11/09 Javascript
Python获取apk文件URL地址实例
2013/11/01 Python
Python中针对函数处理的特殊方法
2014/03/06 Python
零基础写python爬虫之urllib2使用指南
2014/11/05 Python
Python中使用Inotify监控文件实例
2015/02/14 Python
Python编程中运用闭包时所需要注意的一些地方
2015/05/02 Python
django定期执行任务(实例讲解)
2017/11/03 Python
python购物车程序简单代码
2018/04/18 Python
python json.loads兼容单引号数据的方法
2018/12/19 Python
python 删除字符串中连续多个空格并保留一个的方法
2018/12/22 Python
更新pip3与pyttsx3文字语音转换的实现方法
2019/08/08 Python
OpenCV中VideoCapture类的使用详解
2020/02/14 Python
python爬虫中url管理器去重操作实例
2020/11/30 Python
提供世界各地便宜的机票:Sky-tours
2016/07/21 全球购物
美国嘻哈文化生活方式品牌:GLD
2018/04/15 全球购物
丝芙兰意大利官方网站:Sephora.it
2019/12/13 全球购物
销售经理工作职责范文
2013/12/03 职场文书
中学生运动会通讯稿大全
2014/09/18 职场文书
2014年人事专员工作总结
2014/11/19 职场文书
实习指导教师评语
2014/12/30 职场文书
2015年大学生工作总结
2015/04/21 职场文书
《蟋蟀的住宅》教学反思
2016/02/17 职场文书