国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php的mssql數據庫連接類實例

2024-05-04 23:28:13
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php的mssql數據庫連接類,以一個類實例的形式演示了PHP實現針對mssql數據庫的各種常用操作方法,包括對數據庫的連接與增刪改查等操作,非常具有實用價值,需要的朋友可以參考下
 
 

本文實例講述了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程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 津市市| 兴化市| 长武县| 巴中市| 靖宇县| 龙门县| 九台市| 岳池县| 西藏| 铁岭市| 镇坪县| 彰化市| 仁布县| 青河县| 新和县| 施秉县| 洛阳市| 东台市| 福海县| 内黄县| 灵寿县| 色达县| 苍山县| 织金县| 山阴县| 兰考县| 宣汉县| 凤凰县| 噶尔县| 花垣县| 塔河县| 西乌珠穆沁旗| 云安县| 潮安县| 星子县| 黄骅市| 平南县| 许昌县| 鹿邑县| 新巴尔虎右旗| 北海市|