pear/db、adodb、phplib 都还不错,按你的需要选用吧。

解决方案 »

  1.   

    这个是vbb论坛用的
    <?php
    error_reporting(7);
    // db class for mysql
    // this class is used in all scripts
    // do NOT fiddle unless you know what you are doingclass DB_SQL {
      var $database = "";  var $link_id  = 0;
      var $query_id = 0;
      var $record   = array();  var $errdesc  = "";
      var $errno    = 0;
      var $reporterror = 1;  var $server   = "localhost";
      var $user     = "root";
      var $password = "";  function connect() {
        global $usepconnect;
        // connect to db server    if ( 0 == $this->link_id ) {
          if ($this->password=="") {
            if ($usepconnect==1) {
              $this->link_id=mysql_pconnect($this->server,$this->user);
            } else {
              $this->link_id=mysql_connect($this->server,$this->user);
            }
          } else {
            if ($usepconnect==1) {
              $this->link_id=mysql_pconnect($this->server,$this->user,$this->password);
            } else {
              $this->link_id=mysql_connect($this->server,$this->user,$this->password);
            }
          }
          if (!$this->link_id) {
            $this->halt("Link-ID == 连接失败!");
          }
          if ($this->database!="") {
            if(!mysql_select_db($this->database, $this->link_id)) {
              $this->halt("不能选择数据库 ".$this->database);
            }
          }
        }
      }  function geterrdesc() {
        $this->error=mysql_error();
        return $this->error;
      }  function geterrno() {
        $this->errno=mysql_errno();
        return $this->errno;
      }  function select_db($database="") {
        // select database
        if ($database!="") {
          $this->database=$database;
        }    if(!mysql_select_db($this->database, $this->link_id)) {
          $this->halt("不能选择数据库 ".$this->database);
        }  }  function query($query_string) {
        global $query_count,$showqueries,$explain,$querytime;
        // do query
    //$showqueries=1;
        if ($showqueries) {
          echo "Query: $query_string\n";      global $pagestarttime;
          $pageendtime=microtime();
          $starttime=explode(" ",$pagestarttime);
          $endtime=explode(" ",$pageendtime);      $beforetime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];      echo "Time before: $beforetime\n";
        }    $this->query_id = mysql_query($query_string,$this->link_id);
        if (!$this->query_id) {
          $this->halt("Invalid SQL: ".$query_string);
        }    $query_count++;    if ($showqueries) {
          $pageendtime=microtime();
          $starttime=explode(" ",$pagestarttime);
          $endtime=explode(" ",$pageendtime);      $aftertime=$endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
          $querytime+=$aftertime-$beforetime;      echo "Time after:  $aftertime\n";      if ($explain and substr(trim(strtoupper($query_string)),0,6)=="SELECT") {
            $explain_id = mysql_query("EXPLAIN $query_string",$this->link_id);
            echo "</pre>\n";
            echo "
            <table width=100% border=1 cellpadding=2 cellspacing=1>
            <tr>
              <td><b>table</b></td>
              <td><b>type</b></td>
              <td><b>possible_keys</b></td>
              <td><b>key</b></td>
              <td><b>key_len</b></td>
              <td><b>ref</b></td>
              <td><b>rows</b></td>
              <td><b>Extra</b></td>
            </tr>\n";
            while($array=mysql_fetch_array($explain_id)) {
              echo "
              <tr>
                <td>$array[table]&nbsp;</td>
                <td>$array[type]&nbsp;</td>
                <td>$array[possible_keys]&nbsp;</td>
                <td>$array[key]&nbsp;</td>
                <td>$array[key_len]&nbsp;</td>
                <td>$array[ref]&nbsp;</td>
                <td>$array[rows]&nbsp;</td>
                <td>$array[Extra]&nbsp;</td>
              </tr>\n";
            }
            echo "</table>\n<BR><hr>\n";
            echo "\n<pre>";
          } else {
            echo "\n<hr>\n\n";
          }
        }    return $this->query_id;
      }  function fetch_array($query_id=-1,$query_string="") {
        // retrieve row
        if ($query_id!=-1) {
          $this->query_id=$query_id;
        }
        if ( isset($this->query_id) ) {
          $this->record = @mysql_fetch_array($this->query_id);
        } else {
          if ( !empty($query_string) ) {
            $this->halt("Invalid query id (".$this->query_id.") on this query: $query_string");
          } else {
            $this->halt("Invalid query id ".$this->query_id." specified");
          }
        }    return $this->record;
      }  function free_result($query_id=-1) {
        // retrieve row
        if ($query_id!=-1) {
          $this->query_id=$query_id;
        }
        return @mysql_free_result($this->query_id);
      }  function query_first($query_string) {
        // does a query and returns first row
        $query_id = $this->query($query_string);
        $returnarray=$this->fetch_array($query_id, $query_string);
        $this->free_result($query_id);
        return $returnarray;
      }  function data_seek($pos,$query_id=-1) {
        // goes to row $pos
        if ($query_id!=-1) {
          $this->query_id=$query_id;
        }
        return mysql_data_seek($this->query_id, $pos);
      }  function num_rows($query_id=-1) {
        // returns number of rows in query
        if ($query_id!=-1) {
          $this->query_id=$query_id;
        }
        return mysql_num_rows($this->query_id);
      }  function num_fields($query_id=-1) {
        // returns number of fields in query
        if ($query_id!=-1) {
          $this->query_id=$query_id;
        }
        return mysql_num_fields($this->query_id);
      }  function insert_id() {
        // returns last auto_increment field number assigned    return mysql_insert_id($this->link_id);  }
      
      function close() {
       // closes connection to the database

    return mysql_close();
      }  function halt($msg) {
        $this->errdesc=mysql_error();
        $this->errno=mysql_errno();
        // prints warning message when there is an error
        global $technicalemail, $bbuserinfo, $scriptpath, $HTTP_SERVER_VARS;    if ($this->reporterror==1) {
          $message="Database error in " . " $GLOBALS[templateversion]:\n\n$msg\n";
          $message.="mysql error: " . $this->errdesc . "\n\n";
          $message.="mysql error number: " . $this->errno . "\n\n";
          $message.="Date: ".date("l dS of F Y h:i:s A")."\n";
          $message.="Script: $GLOBALS[bburl]" . (($scriptpath) ? $scriptpath : $HTTP_SERVER_VARS['REQUEST_URI']) . "\n";
          $message.="Referer: ".$HTTP_SERVER_VARS['HTTP_REFERER']."\n";    //  if ($technicalemail) {
         //   @mail ($technicalemail,$this->appshortname. " Database error!",$message,"From: $technicalemail");
         // }      echo "<html><head><title>$GLOBALS[bbtitle] Database Error</title><style>P,BODY{FONT-FAMILY:tahoma,arial,sans-serif;FONT-SIZE:11px;}</style><body>\n\n<!-- $message -->\n\n";
       echo "</table></td></tr></table></form>\n<blockquote><p>&nbsp;</p><p><b>$GLOBALS[bbtitle] 数据库好象发生了一些微小的错误.</b><br>\n";
        //---阿宾添加的调试时使用的出错输出
      echo "<pre> $message</pre>" ;
      echo "请按浏览器的 <a href=\"javascript:window.location=window.location;\">刷新</a> 按钮重试.</p>";
          echo "一封E-Mail已经发送到我们的 <a href=\"mailto:$technicalemail\">技术支持信箱</a>, 如果问题仍然存在, 你也可以发邮件联系.</p>";
          echo "<p>我们为由此给你带来不便深感抱歉.</p>";      if ($bbuserinfo['usergroupid']==6) {
            echo "<form><textarea rows=\"12\" cols=\"60\">".htmlspecialchars($message)."</textarea></form>";
          }   echo "</blockquote></body></head></html>";
          exit;
        }
      }
    }
    ?>
      

  2.   

    这个是仿写的,想做别的类的基类 还没有测试:
    <?PHP
    /*
    the database error and log
    1. database doing
    数据库连接:db_connect();db_close();
    基本函数:query();free_result();fetch_array();
    扩展函数:query_first();query_array();
    造作函数:query_new();query_edit();query_del();
    其他: num_rows();affected_rows();insert_id(); 
    2.error 
    错误类型:1-数据库 ,2-没有找到,3-未登陆,4-没有权限。
    函数,5-代码。make_error();
    3.log
      日志类型:系统登陆日志,系统数据库日志,出错日志。
      函数:log();
    */
    CLASS  app{
    var $db_link='';
    var $rs='';
    //----------for database --------------------------
    function db_connect($host='localhost',$user='root',$pass='',$dbname='myapp'){
    $this->db_link=mysql_connect($host,$user,$pass);
    if(!$this->db_link){
    $this->err_end("不能连接数据库!");
    }
    if($dbname!=''){
    if(!mysql_select_db())
    $this->err_end("不能选择数据库!");
    }
    }
    function db_close(){
    return mysql_close();
    }
    function query($SQL){
    $this->rs=mysql_query($SQL);
    if(!$this->rs){
    $ErrMsg="你的数据可能有些问题:<pre>QUERY_String:".$SQL;
    $ErrMsg.="\nERRORNO:".mysql_errno();
    $ErrMsg."\n ERRORMSG:".mysql_error();
    $this->err_end($ErrMsg);
    }
    return $this->rs;
    }
    function free_result($RS=-1){
    if($RS!=-1)$this->rs=$RS;
    return @mysql_free_result($this->rs);
    }
    function fetch_array($RS=-1){
    var $record;
    if($RS!=-1)$this->rs=$RS;
    if(isset($this->rs)){
    $record=@mysql_fetch_array($this->rs);
    }
    else{
    $this->err_end("数据库查询失败");
    }
    }
    function query_first($SQL){
    $this->rs=$this->query($SQL);
    $returnarray=$this->fetch_array($this->rs);
    $this->free_result($this->rs);
    return $returnarray;
    }
    function query_array($SQL){
    $this->rs=$this->query($SQL);
    while($tmp=$this->fetch_array($this->rs))$returnarray[]=$tmp;
    $this->free_result($this->rs);
    return $returnarray;
    }
    function query_new($Table='',$FArray=''){
    var $SQL1='INSERT INTO $Table(';
    var $SQL2=") VALUES(";
    foreach($FArray as $key=>$value){
    $SQL1.=$key.',';
    $SQL2.="'$value',";
    }
    $SQL1=substr($SQL1,0,-1);
    $SQL2=substr($SQL2,0,-1);
    $SQL1.=$SQL2.");";
    $this->rs=$this->query($SQL1);
    return $this->insert_id();
    }
    function query_edit($Table='',$FArray='',$Condition=''){
    var $SQL1='UPDATE $Table SET';
    foreach($FArray as $key=>$value){
    $SQL1.="$key='$value',";
    }
    $SQL1=substr($SQL1,0,-1);
    if(empty($Condition))
    $SQL1.=" ;";
    else 
    $SQL1.=" WHERE $Condition ;";
    $this->rs=$this->query($SQL1);
    return $this->affected_rows();
    }
    function query_del($Table='',$Condition=''){
    var $SQL1="DELETE FROM $Table ";
    if(!empty($Condition))
    $SQL1.="WHERE $Condition;";
    $this->rs=$this->query($SQL1);
    return $this->affected_rows();
    }
    function num_rows($RS=-1){
    if($RS!=-1)
    $this->rs=$RS;
    return @mysql_num_rows($this->rs);
    }
    function affected_rows($RS=-1){
    if($RS!=-1)
    $this->rs=$RS;
    return @mysql_affected_rows($this->rs);
    }
    function insert_id(){
        return @mysql_insert_id($this->rs);
    }
    //+------------for error--------------------------
    function make_error($no='',$msg=''){
    return 1;
    }
    //+------------for log ------------------------
    function log($Type='',$Msg='',$User=''){
    return '';
    }
    function err_end($Reason){
    print($Reason);
    print $this->errmsg;
    exit;
    }
    }
    ?>
      

  3.   

    我需要的不是phplib和vbb论坛所用的那种连接数据库的类。
    xuzuning(唠叨)能不能把pear/db、adodb详细说一下。