db_mysql.inc
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:10k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. /*
  3.  * Session Management for PHP3
  4.  *
  5.  * Copyright (c) 1998,1999 SH Online Dienst GmbH
  6.  *                    Boris Erdmann, Kristian Koehntopp
  7.  *
  8.  * $Id: db_mysql.inc,v 1.2 2000/04/11 14:46:24 prenagha Exp $
  9.  *
  10.  */ 
  11. class DB_Sql {
  12.   
  13.   /* public: connection parameters */
  14.   var $Host     = "";
  15.   var $Database = "";
  16.   var $User     = "";
  17.   var $Password = "";
  18.   /* public: configuration parameters */
  19.   var $Auto_Free     = 0;     ## Set to 1 for automatic mysql_free_result()
  20.   var $Debug         = 0;     ## Set to 1 for debugging messages.
  21.   var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  22.   var $Seq_Table     = "db_sequence";
  23.   /* public: result array and current row number */
  24.   var $Record   = array();
  25.   var $Row;
  26.   /* public: current error number and error text */
  27.   var $Errno    = 0;
  28.   var $Error    = "";
  29.   /* public: this is an api revision, not a CVS revision. */
  30.   var $type     = "mysql";
  31.   var $revision = "1.2";
  32.   /* private: link and query handles */
  33.   var $Link_ID  = 0;
  34.   var $Query_ID = 0;
  35.   
  36.   /* public: constructor */
  37.   function DB_Sql($query = "") {
  38.       $this->query($query);
  39.   }
  40.   /* public: some trivial reporting */
  41.   function link_id() {
  42.     return $this->Link_ID;
  43.   }
  44.   function query_id() {
  45.     return $this->Query_ID;
  46.   }
  47.   /* public: connection management */
  48.   function connect($Database = "", $Host = "", $User = "", $Password = "") {
  49.     /* Handle defaults */
  50.     if ("" == $Database)
  51.       $Database = $this->Database;
  52.     if ("" == $Host)
  53.       $Host     = $this->Host;
  54.     if ("" == $User)
  55.       $User     = $this->User;
  56.     if ("" == $Password)
  57.       $Password = $this->Password;
  58.       
  59.     /* establish connection, select database */
  60.     if ( 0 == $this->Link_ID ) {
  61.     
  62.       $this->Link_ID=mysql_pconnect($Host, $User, $Password);
  63.       if (!$this->Link_ID) {
  64.         $this->halt("pconnect($Host, $User, $Password) failed.");
  65.         return 0;
  66.       }
  67.       if (!@mysql_select_db($Database,$this->Link_ID)) {
  68.         $this->halt("cannot use database ".$this->Database);
  69.         return 0;
  70.       }
  71.     }
  72.     
  73.     return $this->Link_ID;
  74.   }
  75.   /* public: discard the query result */
  76.   function free() {
  77.       @mysql_free_result($this->Query_ID);
  78.       $this->Query_ID = 0;
  79.   }
  80.   /* public: perform a query */
  81.   function query($Query_String) {
  82.     /* No empty queries, please, since PHP4 chokes on them. */
  83.     if ($Query_String == "")
  84.       /* The empty query string is passed on from the constructor,
  85.        * when calling the class without a query, e.g. in situations
  86.        * like these: '$db = new DB_Sql_Subclass;'
  87.        */
  88.       return 0;
  89.     if (!$this->connect()) {
  90.       return 0; /* we already complained in connect() about that. */
  91.     };
  92.     # New query, discard previous result.
  93.     if ($this->Query_ID) {
  94.       $this->free();
  95.     }
  96.     if ($this->Debug)
  97.       printf("Debug: query = %s<br>n", $Query_String);
  98.     $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
  99.     $this->Row   = 0;
  100.     $this->Errno = mysql_errno();
  101.     $this->Error = mysql_error();
  102.     if (!$this->Query_ID) {
  103.       $this->halt("Invalid SQL: ".$Query_String);
  104.     }
  105.     # Will return nada if it fails. That's fine.
  106.     return $this->Query_ID;
  107.   }
  108.   /* public: walk result set */
  109.   function next_record() {
  110.     if (!$this->Query_ID) {
  111.       $this->halt("next_record called with no query pending.");
  112.       return 0;
  113.     }
  114.     $this->Record = @mysql_fetch_array($this->Query_ID);
  115.     $this->Row   += 1;
  116.     $this->Errno  = mysql_errno();
  117.     $this->Error  = mysql_error();
  118.     $stat = is_array($this->Record);
  119.     if (!$stat && $this->Auto_Free) {
  120.       $this->free();
  121.     }
  122.     return $stat;
  123.   }
  124.   /* public: position in result set */
  125.   function seek($pos = 0) {
  126.     $status = @mysql_data_seek($this->Query_ID, $pos);
  127.     if ($status)
  128.       $this->Row = $pos;
  129.     else {
  130.       $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
  131.       /* half assed attempt to save the day, 
  132.        * but do not consider this documented or even
  133.        * desireable behaviour.
  134.        */
  135.       @mysql_data_seek($this->Query_ID, $this->num_rows());
  136.       $this->Row = $this->num_rows;
  137.       return 0;
  138.     }
  139.     return 1;
  140.   }
  141.   /* public: table locking */
  142.   function lock($table, $mode="write") {
  143.     $this->connect();
  144.     
  145.     $query="lock tables ";
  146.     if (is_array($table)) {
  147.       while (list($key,$value)=each($table)) {
  148.         if ($key=="read" && $key!=0) {
  149.           $query.="$value read, ";
  150.         } else {
  151.           $query.="$value $mode, ";
  152.         }
  153.       }
  154.       $query=substr($query,0,-2);
  155.     } else {
  156.       $query.="$table $mode";
  157.     }
  158.     $res = @mysql_query($query, $this->Link_ID);
  159.     if (!$res) {
  160.       $this->halt("lock($table, $mode) failed.");
  161.       return 0;
  162.     }
  163.     return $res;
  164.   }
  165.   
  166.   function unlock() {
  167.     $this->connect();
  168.     $res = @mysql_query("unlock tables");
  169.     if (!$res) {
  170.       $this->halt("unlock() failed.");
  171.       return 0;
  172.     }
  173.     return $res;
  174.   }
  175.   /* public: evaluate the result (size, width) */
  176.   function affected_rows() {
  177.     return @mysql_affected_rows($this->Link_ID);
  178.   }
  179.   function num_rows() {
  180.     return @mysql_num_rows($this->Query_ID);
  181.   }
  182.   function num_fields() {
  183.     return @mysql_num_fields($this->Query_ID);
  184.   }
  185.   /* public: shorthand notation */
  186.   function nf() {
  187.     return $this->num_rows();
  188.   }
  189.   function np() {
  190.     print $this->num_rows();
  191.   }
  192.   function f($Name) {
  193.     return $this->Record[$Name];
  194.   }
  195.   function p($Name) {
  196.     print $this->Record[$Name];
  197.   }
  198.   /* public: sequence numbers */
  199.   function nextid($seq_name) {
  200.     $this->connect();
  201.     
  202.     if ($this->lock($this->Seq_Table)) {
  203.       /* get sequence number (locked) and increment */
  204.       $q  = sprintf("select nextid from %s where seq_name = '%s'",
  205.                 $this->Seq_Table,
  206.                 $seq_name);
  207.       $id  = @mysql_query($q, $this->Link_ID);
  208.       $res = @mysql_fetch_array($id);
  209.       
  210.       /* No current value, make one */
  211.       if (!is_array($res)) {
  212.         $currentid = 0;
  213.         $q = sprintf("insert into %s values('%s', %s)",
  214.                  $this->Seq_Table,
  215.                  $seq_name,
  216.                  $currentid);
  217.         $id = @mysql_query($q, $this->Link_ID);
  218.       } else {
  219.         $currentid = $res["nextid"];
  220.       }
  221.       $nextid = $currentid + 1;
  222.       $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  223.                $this->Seq_Table,
  224.                $nextid,
  225.                $seq_name);
  226.       $id = @mysql_query($q, $this->Link_ID);
  227.       $this->unlock();
  228.     } else {
  229.       $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
  230.       return 0;
  231.     }
  232.     return $nextid;
  233.   }
  234.   /* public: return table metadata */
  235.   function metadata($table='',$full=false) {
  236.     $count = 0;
  237.     $id    = 0;
  238.     $res   = array();
  239.     /*
  240.      * Due to compatibility problems with Table we changed the behavior
  241.      * of metadata();
  242.      * depending on $full, metadata returns the following values:
  243.      *
  244.      * - full is false (default):
  245.      * $result[]:
  246.      *   [0]["table"]  table name
  247.      *   [0]["name"]   field name
  248.      *   [0]["type"]   field type
  249.      *   [0]["len"]    field length
  250.      *   [0]["flags"]  field flags
  251.      *
  252.      * - full is true
  253.      * $result[]:
  254.      *   ["num_fields"] number of metadata records
  255.      *   [0]["table"]  table name
  256.      *   [0]["name"]   field name
  257.      *   [0]["type"]   field type
  258.      *   [0]["len"]    field length
  259.      *   [0]["flags"]  field flags
  260.      *   ["meta"][field name]  index of field named "field name"
  261.      *   The last one is used, if you have a field name, but no index.
  262.      *   Test:  if (isset($result['meta']['myfield'])) { ...
  263.      */
  264.     // if no $table specified, assume that we are working with a query
  265.     // result
  266.     if ($table) {
  267.       $this->connect();
  268.       $id = @mysql_list_fields($this->Database, $table);
  269.       if (!$id)
  270.         $this->halt("Metadata query failed.");
  271.     } else {
  272.       $id = $this->Query_ID; 
  273.       if (!$id)
  274.         $this->halt("No query specified.");
  275.     }
  276.  
  277.     $count = @mysql_num_fields($id);
  278.     // made this IF due to performance (one if is faster than $count if's)
  279.     if (!$full) {
  280.       for ($i=0; $i<$count; $i++) {
  281.         $res[$i]["table"] = @mysql_field_table ($id, $i);
  282.         $res[$i]["name"]  = @mysql_field_name  ($id, $i);
  283.         $res[$i]["type"]  = @mysql_field_type  ($id, $i);
  284.         $res[$i]["len"]   = @mysql_field_len   ($id, $i);
  285.         $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  286.       }
  287.     } else { // full
  288.       $res["num_fields"]= $count;
  289.     
  290.       for ($i=0; $i<$count; $i++) {
  291.         $res[$i]["table"] = @mysql_field_table ($id, $i);
  292.         $res[$i]["name"]  = @mysql_field_name  ($id, $i);
  293.         $res[$i]["type"]  = @mysql_field_type  ($id, $i);
  294.         $res[$i]["len"]   = @mysql_field_len   ($id, $i);
  295.         $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  296.         $res["meta"][$res[$i]["name"]] = $i;
  297.       }
  298.     }
  299.     
  300.     // free the result only if we were called on a table
  301.     if ($table) @mysql_free_result($id);
  302.     return $res;
  303.   }
  304.   /* private: error handling */
  305.   function halt($msg) {
  306.     $this->Error = @mysql_error($this->Link_ID);
  307.     $this->Errno = @mysql_errno($this->Link_ID);
  308.     if ($this->Halt_On_Error == "no")
  309.       return;
  310.     $this->haltmsg($msg);
  311.     if ($this->Halt_On_Error != "report")
  312.       die("Session halted.");
  313.   }
  314.   function haltmsg($msg) {
  315.     printf("</td></tr></table><b>Database error:</b> %s<br>n", $msg);
  316.     printf("<b>MySQL Error</b>: %s (%s)<br>n",
  317.       $this->Errno,
  318.       $this->Error);
  319.   }
  320.   function table_names() {
  321.     $this->query("SHOW TABLES");
  322.     $i=0;
  323.     while ($info=mysql_fetch_row($this->Query_ID))
  324.      {
  325.       $return[$i]["table_name"]= $info[0];
  326.       $return[$i]["tablespace_name"]=$this->Database;
  327.       $return[$i]["database"]=$this->Database;
  328.       $i++;
  329.      }
  330.    return $return;
  331.   }
  332. }
  333. ?>