找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 672|回复: 0
打印 上一主题 下一主题

php的mssql数据库连接类实例

[复制链接]

2560

主题

2560

帖子

7622

积分

论坛元老

Rank: 8Rank: 8

积分
7622
跳转到指定楼层
楼主
发表于 2018-2-14 05:52:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了php的mssql数据库连接类实例代码,分享给大家供大家参考。
具体实现代码如下:
[U]复制代码[/U] 代码如下: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->assword);
      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("
Debug: query = %s
", $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[]
      $count = mssql_num_fields($this->Query_ID);
      for ($i=0; $iQuery_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; $ifree_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]Database error: %s
", $msg);
    printf("MSSQL Error: %s (%s)
",
      $this->Errno,
      $this->Error);
    die("Session halted.");
  }
}
希望本文所述对大家的PHP程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • php adodb连接mssql解决乱码问题
  • php 连接mssql数据库 初学php笔记
  • php下连接mssql2005的代码
  • 关于php连接mssql:pdo odbc sql server
  • php连接mssql的一些相关经验及注意事项
  • php连接mssql数据库的几种方法
  • PHP连接MSSQL2008/2005数据库(SQLSRV)配置实例
  • php使用pdo连接mssql server数据库实例
  • php5.3不能连接mssql数据库的解决方法
  • PHP连接MSSQL方法汇总
  • php基于PDO连接MSSQL示例DEMO
  • PHP基于mssql扩展远程连接MSSQL的简单实现方法
  • php连接微软MSSQL(sql server)完全攻略
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端