从phplib修改的mysql操作的类
非常好的class
<?
class DB_Sql {  
  var 
$Host     'localhost'//主机名称
  
var $Database 'test'//数据库名称
  
var $User     'root'//用户名
  
var $Password ''//用户密码
  /* public: configuration parameters */
  
var $Auto_Free     0;     // 如果非0就自动释放Mysql
  
var $Debug         0;     // 如果非0就显示调试信息
  
var $Halt_On_Error 'yes'// "yes"(halt with message),"no"(ignore errors quietly),"report"(ignore errror, but spit a warning)
  
var $Seq_Table     'db_sequence';
  var 
$Record        = array();
  var 
$Row;
  var 
$Errno         0;
  var 
$Error         '';
  
/* 本MySQL操作库的版本. */
  
var $type     "mysql";
  var 
$revision "1.0CVersion";
  var 
$Link_ID  0;
  var 
$Query_ID 0;
  
/* 下面开始过程部分 */
  /* 默认连接 */
  
function DB_SQL($Database ''$Host ''$User ''$Password '') {
    
$this->connect($Database$Host$User$Password);
  }
  
/* 返回连接指针 */
  
function link_id() {
    return 
$this->Link_ID;
  }
  
/* 返回查询指针 */
  
function query_id() {
    return 
$this->Query_ID;
  }
  
/* 连接MySQL数据库 */
  
function connect($Database ''$Host ''$User ''$Password '') {
    if (
'' <> $Database$this->Database $Database;
    if (
'' <> $Host)     $this->Host     $Host;
    if (
'' <> $User)     $this->User     $User;
    if (
'' <> $Password$this->Password $Password;
    if ( 
== $this->Link_ID ) {
      
$this->Link_ID=mysql_pconnect($this->Host$this->User$this->Password);
      if (!
$this->Link_ID) {
        
$this->halt("MySQLpConnect($this->Host, $this->User, \$this->Password) failed.");
        return 
false;
      }
      if (!@
mysql_select_db($this->Database,$this->Link_ID)) {
        
$this->halt("Cannot use database ".$this->Database);
        return 
false;
      }
    }
    return 
$this->Link_ID;
  }
  
/* 释放MySQL函数 */
  
function free() {
      @
mysql_free_result($this->Query_ID);
      
$this->Query_ID 0;
  }
  
/* MySQL查询 */
  
function query($Query_String) {
    if (
$Query_String == '') return false;
    if (!
$this->connect())   return false;
    if (
$this->Query_ID)     $this->free();
    if (
$this->Debug)        printf("Debug: query = %s<br>\n"$Query_String);
    
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
    
$this->Row      0;
    
$this->Errno    mysql_errno();
    
$this->Error    mysql_error();
    if (!
$this->Query_ID)    $this->halt("Invalid SQL: ".$Query_String);
    return 
$this->Query_ID;
  }
  
/* 把结果指针往前推进 */
  
function next_record() {
    if (!
$this->Query_ID) {
      
$this->halt("Next_record called with no query pending.");
      return 
false;
    }
    
$this->Record = @mysql_fetch_array($this->Query_ID);
    
$this->Errno  mysql_errno();
    
$this->Error  mysql_error();
    
$this->Row++;
    
$stat         is_array($this->Record);
    if (!
$stat && $this->Auto_Free$this->free();
    return 
$stat;
  }
  
/* 偏移指针 */
  
function seek($pos 0) {
    
$status = @mysql_data_seek($this->Query_ID$pos);
    if (
$status$this->Row $pos;
    else {
      
$this->halt("Seek($pos) failed: result has ".$this->num_rows()." rows");
      @
mysql_data_seek($this->Query_ID$this->num_rows());
      
$this->Row $this->num_rows;
      return 
false;
    }
    return 
true;
  }
  
/*----------------------------------------------*
   * 锁表格:注意                                 *
   * 本表格可以是一个数组                         *
   * 如果是数组就会把所有数组元素当作表格来处理   *
   *--------------------------------------------- */
  
function lock($table$mode='write') {
    
$this->connect();    
    
$query='lock tables ';
    if (
is_array($table)) {
      while (list(
$key,$value)=each($table)) {
        if (
$key=='read' && $key!=0) {
          
$query.="$value read, ";
        } else {
          
$query.="$value $mode, ";
        }
      }
      
$query substr($query,0,-2);
    } else {
      
$query.="$table $mode";
    }
    
$res = @mysql_query($query$this->Link_ID);
    if (!
$res) {
      
$this->halt("lock($table, $mode) failed.");
      return 
false;
    }
    return 
$res;
  }
  
/* 表格解锁 */
  
function unlock() {
    
$this->connect();
    
$res = @mysql_query("unlock tables");
    if (!
$res) {
      
$this->halt("unlock() failed.");
      return 
false;
    }
    return 
$res;
  }
  
/* 返回前一次操作改动的行数 */
  
function affected_rows() {
    return @
mysql_affected_rows($this->Link_ID);
  }
  
/* 返回符合查询结果的行数 */
  
function num_rows() {
    return @
mysql_num_rows($this->Query_ID);
  }
  
/* 返回查询结果的列的数目 */
  
function num_fields() {
    return @
mysql_num_fields($this->Query_ID);
  }
  
/* 返回查询结果中Name字段的值 */
  
function f($Name) {
    return 
$this->Record[$Name];
  }
  
/* 打印查询结果中Name字段的值 */
  
function p($Name) {
    print 
$this->Record[$Name];
  }
  
/* 以数组的形式返回查询结果 */
  
function fetch_array() {
    return 
$this->Record;
  }
  
/* 数据库的信息,原封未动 */
  
function metadata($table='',$full=false) {
    
$count 0;
    
$id    0;
    
$res   = array();
    
/*
     * Due to compatibility problems with Table we changed the behavior
     * of metadata();
     * depending on $full, metadata returns the following values:
     *
     * - full is false (default):
     * $result[]:
     *   [0]["table"]  table name
     *   [0]["name"]   field name
     *   [0]["type"]   field type
     *   [0]["len"]    field length
     *   [0]["flags"]  field flags
     *
     * - full is true
     * $result[]:
     *   ["num_fields"] number of metadata records
     *   [0]["table"]  table name
     *   [0]["name"]   field name
     *   [0]["type"]   field type
     *   [0]["len"]    field length
     *   [0]["flags"]  field flags
     *   ["meta"][field name]  index of field named "field name"
     *   The last one is used, if you have a field name, but no index.
     *   Test:  if (isset($result['meta']['myfield'])) { ...
     */

    // if no $table specified, assume that we are working with a query
    // result
    
if ($table) {
      
$this->connect();
      
$id = @mysql_list_fields($this->Database$table);
      if (!
$id)
        
$this->halt("Metadata query failed.");
    } else {
      
$id $this->Query_ID
      if (!
$id)
        
$this->halt("No query specified.");
    } 
    
$count = @mysql_num_fields($id);
    
// made this IF due to performance (one if is faster than $count if's)
    
if (!$full) {
      for (
$i=0$i<$count$i++) {
        
$res[$i]["table"] = @mysql_field_table ($id$i);
        
$res[$i]["name"]  = @mysql_field_name  ($id$i);
        
$res[$i]["type"]  = @mysql_field_type  ($id$i);
        
$res[$i]["len"]   = @mysql_field_len   ($id$i);
        
$res[$i]["flags"] = @mysql_field_flags ($id$i);
      }
    } else { 
// full
      
$res["num_fields"]= $count;    
      for (
$i=0$i<$count$i++) {
        
$res[$i]["table"] = @mysql_field_table ($id$i);
        
$res[$i]["name"]  = @mysql_field_name  ($id$i);
        
$res[$i]["type"]  = @mysql_field_type  ($id$i);
        
$res[$i]["len"]   = @mysql_field_len   ($id$i);
        
$res[$i]["flags"] = @mysql_field_flags ($id$i);
        
$res["meta"][$res[$i]["name"]] = $i;
      }
    }    
    
// free the result only if we were called on a table
    
if ($table) @mysql_free_result($id);
    return 
$res;
  }
  
/*-------------------------------------*
   * 返回数据库中所有的表格名称,说明:  *
   * 将返回一个数组,其中包括了所有的表  *
   * 格,每个表格是数组的一个元素,每个  *
   * 元素又是一个数组,包括了teble_name  *
   * 和database两个元素,table_name是表  *
   * 格的名称,database是数据库的名称。  *
   *-------------------------------------*/
  
function table_names() {
    
$this->query("SHOW TABLES");
    
$i=0;
    while (
$info=mysql_fetch_row($this->Query_ID)) {
      
$return[$i]["table_name"]= $info[0];
      
$return[$i]["database"]=$this->Database;
      
$i++;
    }
    return 
$return;
  }
  
/* 错误信息处理函数,下面都是 */
  
function halt($msg) {
    
$this->Error = @mysql_error($this->Link_ID);
    
$this->Errno = @mysql_errno($this->Link_ID);
    if (
$this->Halt_On_Error == "no")
      return;
    
$this->haltmsg($msg);
    if (
$this->Halt_On_Error != "report")
      die(
"Session halted.");
  }
  function 
haltmsg($msg) {
    
printf("</td></tr></table><b>Database error:</b> %s<br>\n"$msg);
    
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
      
$this->Errno,
      
$this->Error);
  }
  
/* 栏目增加和减少项目的函数 */
  
function counts($seq_name$mode '+') {
    
$this->connect();
    if (
$this->lock($this->Seq_Table)) {
      
$q   sprintf("select num from %s where seq_name = '%s'"$this->Seq_Table$seq_name);
      
$id  = @mysql_query($q$this->Link_ID);
      
$res = @mysql_fetch_array($id);
      
//如果没有这个row,建立一个
      
if (!is_array($res)) {
        
$currentid 0;
        
$q sprintf("insert into %s values('%s', %s)"$this->Seq_Table$seq_name$currentid);
        
$id = @mysql_query($q$this->Link_ID);
      }
      if(
$mode=='+') {
        
$this->id_add($seq_name);
        
$count $res[0]+1;
      } else {
        
$this->id_minus($seq_name);
        
$count $res[0]-1;
      }
      
$this->unlock();
    } else {
      
$this->halt("Cannot lock ".$this->Seq_Table." - has it been created?");
      return 
false;
    }
    return 
$count;
  }
  function 
id_add($seq_name) {
    
$q sprintf("update %s set num = num+1 where seq_name = '%s'"$this->Seq_Table$seq_name);
    
$current = @mysql_query($q$this->Link_ID);
    return 
$current;
  }
  function 
id_minus($seq_name) {
    
$q sprintf("update %s set num = num-1 where seq_name = '%s'"$this->Seq_Table$seq_name);
    
$current = @mysql_query($q$this->Link_ID);
    return 
$current;
  }
// Class DB_SQL Ended
}
/*----------------------------------------------------------------------------
  id_minus  id_add和nextid的用法:
  必须建立一个表格,表格名称为Seq_Table的值
  其中seq_name代表项目名称,num代表数目
----------------------------------------------------------------------------*/
?>