首页 > 开发 > PHP > 正文

Db_mssql_class

2024-05-04 22:59:40
字体:
来源:转载
供稿:网友

详细介绍:
   由于工作的原因,需要对sql server数据库进行操作,根据以前使用的mysql数据库操作类改写成现在这个对sql server进行操作的php类,可以执行连接数据库,执行sql语句,查询数据,获得最后一次插入操作的id号等功能!

<?php
/****************************************************************************
            db_mssql_class.php  -  description
                             -------------------
    begin                : 2002 4 2

    $id: db_mssql_class.php,v 1.1 2002/04/03 09:25:33 simon.qiu exp $
/****************************************************************************/

class db_handle{

 var $classname = "db_handle";
 
 var $server;
 var $username;
 var $password;
 var $database;
 
 var $linkid=0;
 var $queryresult="";
 var $lastinsertid = "";
 
 /* private ignore=>ignore the error and continue, halt=>report the error and halt, report=>report the error and continue */
 var $halt_on_error = "report";
 
 var $error = "";
 var $errno = 0;
 
 /**public
 *  remark: this is the db_mysql_class's structure
 *  function: set the server,username,password,database variable.
 */
 function db_handle($server="",$username="",$password="",$database=""){
  $this->server = $server;
  $this->username = $username;
  $this->password = $password;
  $this->database = $database;
 }
 
   
 /**public
 *  function: connect database and select database
 *  success: retun 1
 *  failed: return 0 
 */
 function connect(){
  $this->linkid = @mssql_pconnect($this->server,$this->username,$this->password);
  if(!$this->linkid){
   $this->halt("mssql_pconnect($this->server,$this->username,$this->password): failed");
   return 0;
  }
  if([email protected]_select_db($this->database)){
   $this->halt("mssql_select_db($this->database) failed.");
   return 0;
  }
  return 1;
 }
 
 
 /**public
 *  function: check the database, if exist then select
 *  exist: return 1
 *  not exist: return 0
 */
 function selectdatabase(){
  if(@mssql_select_db($this->database))
   return 1;
  else
   return 0;
 }

 /**public
 *  function: execute sql instruction
 *  success: return sql result.
 *  failed: return 0;
 */
 function execquery($sql=""){
  if($this->linkid == 0){
   $this->halt("execute sql failed: hava not valid database connect.");
   return 0;
  }
 
  ob_start();
  $this->queryresult = mssql_query($sql,$this->linkid);
  $error = ob_get_contents();
  ob_end_clean();
  if($error){
  
   $this->halt("execute sql: mssql_query($sql,$this->linkid) failed.");
   return 0;
  }
  $reg = "#insert into#";
  if(preg_match($reg,$sql)){
   $sql = "select @@identity as id";
   $res = mssql_query($sql,$this->linkid);
   $this->lastinsertid = mssql_result($res,0,id);
  }
  return $this->queryresult;
 }
 
 
 /**public
 *  function: get the query result's row number
 *  success: return the row fo the result
 *  failed: return 0
 */
 function gettotalrownum($result=""){
  if($result != "") $this->queryresult = $result;
  $row = @mssql_num_rows($this->queryresult);
  if($row >= 0) return $row;
  $this->halt("get a row of result failed: result $result is invalid.");
  return 0;
 }
 
 
 /**public
 *  function: get the last insert record's id
 *  success: return id
 *  failed: return 0
 */
 function lastinsertid(){
  return $this->lastinsertid;
 }
 
 
 /**public
 *  function: get a field's value
 *  success: return value of the field
 *  failed: return 0
 */
 function getfield($result="",$row=0,$field=0){
  if($result != "") $this->queryresult = $result;
  $fieldvalue = @mssql_result($this->queryresult,$row,$field);
  if($fieldvalue != "") return $fieldvalue;
  $this->halt("get field: mssql_result($this->queryresult,$row,$field) failed.");
  return 0;
 
  //here should have error handle 
 }
 
 
 /**public
 *  function: get next record
 *  success: return a array of the record's value
 *  failed: return 0
 */
 function nextrecord($result=""){
  if($result != "") $this->queryresult = $result;
  $record = @mssql_fetch_array($this->queryresult);
  if(is_array($record)) return $record;
  //$this->halt("get the next record failed: the result $result is invalid.");
  return 0;
 }
 
 
 /**public
 *  function: free the query result
 *  success return 1
 *  failed: return 0
 */
 function freeresult($result=""){
  if($result != "") $this->queryresult = $result;
  return @mssql_free_result($this->queryresult);
 }
 
 
 /**public
 *  function: set the halt_on_error's state
 *  success: return 1
 *  failed: return 0
 */
 function sethaltonerror($state="ignore"){
  if(!($state == "ignore" || $state == "report" || $state == "halt")){
   $this->halt("set the halt_on_error fail: there is no state value $state");
   return 0;     
  }
  $this->halt_on_error = $state;
  return 1;
 }
 
 
 /**public
 *  function: get the halt_on_error's state
 */
 function gethaltonerror(){
  return $this->halt_on_error;
 }
 
 
 /**public
 *  function: get the class's name
 */
 function tostring(){
  return $this->classname;
 }
 
 
 /**private
 *  function: error handle
 */
 function halt($msg){
  $this->error = @mysql_error($this->linkid);
  $this->errno = @mysql_errno($this->linkid);
  if ($this->halt_on_error == "ignore") return;
  $this->makemsg($msg);
  if ($this->halt_on_error == "halt") die("<b>session halted</b>");
 }
 
 
 /**private
 *  function: make error information and print
 */
 function makemsg($msg){
  printf("<b>database error:</b> %s<br>/n", $msg);
  printf("<b>mysql error</b>: %s (%s)<br>/n",$this->errno,$this->error);
 }
}

?>

 

  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表