search
Homephp教程php手册php程序来连接SQL Server数据库的通用类

  很多朋友问我如何用php程序来连接SQL Server数据库的通用类,其实这个并不是很难的,关键是一点点仔细,一点点耐心,用激情去理解

  class DB {
   var $Host = "192.168.0.199"; // Hostname of our MySQL server
   var $Database = "test"; // Logical database name on that server
   var $User = "user"; // Database user
   var $Password = "password"; // Database user's password
  
   var $Link_ID = 0; // Result of mssql_connect()
   var $Query_ID = 0; // Result of most recent mssql_query()
   var $Row = 0; // Current row number
   var $Errno = 0; // Error state of query
   var $Error = "";
  
  var $AffNum=0;
  
  /**************************************
   *打印错误方法:显示页面处理的错误信息。
  ****************************************/
  function Halt($msg) {
   printf("Database error: %s
\n", $msg);
   printf("mssql Error: %s (%s)
\n",
   $this->Errno,
   $this->Error);
   die("Session halted.");
   }
  
  /**************************************
   *连接数据库,并且选择默认的数据库
   **************************************/
  function Connect() {
   if ( 0 == $this->Link_ID ) {
  $this->Link_ID=mssql_connect($this->Host,$this->User,$this->Password) or die("Couldn't connect to SQL Server on
  
  $servername");
  Database,$this->Link_ID">$db=@mssql_select_db($this->Database,$this->Link_ID);
   if (!$this->Link_ID) {
   $this->Halt("Link-ID == false, mssql_connect failed");
   }
   }
   }
  
  /****************************************
   *关闭数据库,如果数据库连接已经打开则关闭它
   *请在调用Connect()并处理后使用Close()
   ****************************************/
  function Close() {
  if (0 != $this->Link_ID){
  mssql_close();
  }
  }
  
  /*************************************************
   *输入sql语句,有select,update,insert,delete
   *包括存储过程也可以通过这个方法来调用。
   *************************************************/
  function Query($Query_String) {
   $this->Connect();
  
   $this->Query_ID = mssql_query($Query_String);
   $this->Row = 0;
   if (!$this->Query_ID) {
  $msg=mssql_get_last_message();
  
  if($msg==null || $msg=="")
  {
  $this->AffNum=1;
  return 1;
  }
  
  if(strtolower(substr($Query_String,0,6))!="select"){
  $this->AffNum=1;
  return 1;
  }
  
   $this->Errno = 1;
   $this->Error = "General Error (The mssql interface cannot return detailed error messages)(".$msg.").";
   $this->halt("Invalid SQL: ".$Query_String);
   }
   return $this->Query_ID;
   }
  
  /*******************************************************
   *把查询数据库的指针移到下一条记录
   *******************************************************/
  function NextRecord() {
   $this->Record = array();
  mssql_next_result($this->Query_ID);
  $this->Record=mssql_fetch_array($this->Query_ID);
  
  $result=$this->Record;
  if(!is_array($result)) return $this->Record;
  foreach($result as $key => $value){
  $keylower=strtolower($key);
  if($keylower!=$key) $this->Record[$keylower]=$value;
  }
  
   return $this->Record;
   }
  
  /********************************************************
   *重新定位查询数据库的指针
   ********************************************************/
  function Seek($pos) {
  if($pos  if(eregi("[0-9]",$pos)) mssql_data_seek($this->Query_ID,$pos);
   }
  
  /********************************************************
   *获取查询数据库得到的总行数
   ********************************************************/
  function NumRows() {
  if($this->Query_ID) $num_rows=mssql_num_rows($this->Query_ID);
  else $num_rows=$this->AffNum;
  
   return $num_rows;
   }
  
  /*******************************************************
   *字段数
   *******************************************************/
  function NumFields() {
   return count($this->Record)/2;
   }
  
  /*******************************
   *该字段的值
   *******************************/
  function FieldValue($Field_Name){
  return $this->Record[$Field_Name];
   }
  
  /******************************
   *update,insert,delete影响的行数
   ******************************/
  function AffectedRows() {
  if($this->Query_ID) return mssql_num_rows($this->Query_ID);
  else{
  return $this->AffNum;
  }
   }
  ?>
  
  
  以下是使用示例:
  
  
  //构造新的DB类
  $DBConn=new DB;
  
  //写入sql查询语句
  $SqlStr="select * from test";
  $DBConn->Query($SqlStr);
  
  //循环输出查询得到的结果
  while($Row=$DBConn->NextRecord()){
  echo $Row[testid];
  }
  
  //关闭数据库连接
  $DBConn->Close();
  ?>



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools