class CDB { /* * */ var $Host = "localhost"; ## 主机名 var $Database = "mydb"; ## 数据库名;构造函数可以改变默认数据库名 var $User = "root"; ## 用户名 var $Password = "root"; ## 密码 var $Link_ID = 0; var $Query_ID = 0; var $Record = array(); ## 从数据库中读取的数据 var $Row; var $Errno = 0; var $Error = ""; var $Auto_free = 0; ## Set this to 1 for automatic mysql_free_result() var $Auto_commit = 0; ## set this to 1 to automatically commit results var $debugmode = 0; /* function CDB($Host,$Database,$User,$Password) {## 构造函数,改变$Host,$Database,$User,$Password if (isset($Host)) $this->Host = $Host; if (isset($Database)) $this->Database = $Database ; if (isset($User)) $this->User = $User ; if (isset($Password)) $this->Password = $Password ; }*/ function connect() { ## 连接Mysql数据库 if ( 0 == $this->Link_ID ) { $this->Link_ID=mysql_pconnect($this->Host, $this->User, $this->Password); if (!$this->Link_ID) { $this->halt("Link-ID == false, pconnect failed"); } if (!mysql_query(sprintf("use %s",$this->Database),$this->Link_ID)) { $this->halt("cannot use database ".$this->Database); } } } function query($Query_String) { // 执行Sql语句 $this->connect(); if ($this->debugmode) 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() { $this->Record = mysql_fetch_array($this->Query_ID); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array($this->Record); ## 判断变数型态是否为阵列型态 if (!$stat && $this->Auto_free) { mysql_free_result($this->Query_ID); $this->Query_ID = 0; } return $stat; } function seek($pos) { $status = mysql_data_seek($this->Query_ID, $pos); if ($status) $this->Row = $pos; return; } function metadata($table) { $count = 0; $id = 0; $res = array(); $this->connect(); $id = @mysql_list_fields($this->Database, $table); if ($id < 0) { $this->Errno = mysql_errno(); $this->Error = mysql_error(); $this->halt("Metadata query failed."); } $count = mysql_num_fields($id); 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; $res["num_fields"]= $count; } mysql_free_result($id); 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); } function nf() { ## 取得数据条数 return $this->num_rows(); } function np() { ## 打印数据条数 print $this->num_rows(); } function f($Name) { ## 取得字段内容 return $this->Record[$Name]; } function p($Name) { ## 打印字段内容 print $this->Record[$Name]; } function halt($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); die("Session halted."); } function gen_rand_string($len) { ## return 随机$len位随机字符串;若$len为空,则取8位随机字符串 if (!isset($len)) $len="8"; $chars = array( 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g' , 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o' , 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w' , 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'); $max_chars = count($chars) - 1; srand( (double) microtime()*1000000); $rand_str = ''; for($i = 0; $i < $len; $i++) { $rand_str = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $rand_str . $chars[rand(0, $max_chars)]; } return $rand_str; } function check_reg_user($no_username) { ## 检验是否用户名重复 $sql = "select * from no_user where no_username = '$no_username'"; $this->query($sql); if ($this->nf() == 0) return 1; else return 0; } }