-
-
//Database operation class - class db
- {
- //Data saving variables after SQL execution;
- var $db;
- //Read or set current data Position
- var $position=0;
- //Execute the SQL statement and save the result as a db variable;
function sub_sql($str)
- {
- global $prefix;//Global Function, table prefix
- return str_replace("#@__",$prefix,$str);
- }
- function Sql($str)
- {
- $str=$this->sub_sql($str);
- $result = mysql_query($str);
- $i=0;
- while($row = mysql_fetch_array($result))
- {
- $str_array[$i]=$row;
- $i++;
- }
- if(empty($ str_array))
- {
- $str_array=array();
- }
- $this->db=$str_array;
- }
- //Read a piece of data and move the data back one bit, and return if the data is empty is null;
- function Get_One()
- {
- $re=empty($this->db[$this->position])?null:$this->db[$this->position];
- $this->position=$re?$this->position+1:$this->position;
- return $re;
- }
- //Judge whether the data has been read to the end
- function Judge()
- {
- $re=empty($this->db[$this->position])?true:false;
- return $re;
- }
- //Get the number in db
- function Get_Num()
- {
- return count($this->db);
- }
- //Update the data in the database, $t is the table name, $v format is the array format, the superscript is the field name, and the subscript is the data; $w is The superscript of the condition is the field name and the subscript is the data. $p is the condition. 0 is the equal sign, 1 is greater than, -1 is less than;
- function Set_Updata($t,$v,$w,$p=0)
- {
- $this->Sql($t);
- $v_str="";
- $w_str="";
- $f="";
- foreach($v as $key=>$vaule)
- {
- if (!is_numeric($key))
- {
- if(empty($v_str))
- {
- $v_str=htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
- }else
- {
- $v_str=$v_str.",".htmlspecialchars($key)."='".htmlspecialchars($vaule)."'";
- }
- }
- }
- switch($p)
- {
- case 0 :
- $f="=";
- break;
- case 1:
- $f=">";
- break;
- case -1:
- $f="<";
- break;
- }
- if( !empty($f))
- {
- foreach($w as $key=>$vaule)
- {
- if(!is_numeric($key))
- {
- if(empty($v_str))
- {
- $ w_str=htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
- }else
- {
- $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule )."'";
- }
- }
- }
- }
- $sql="UPDATE ".$t." SET ".$v_str." where ".$w_str;
- return $result = mysql_query($sql);
- }
- //Delete a piece of data. $w is the condition. The superscript is the field name and the subscript is the data. $p is the condition. 0 is the equal sign, 1 is greater than, -1 is less than;
- function Set_Del($t,$w, $p=0)
- {
- $this->sub_sql($t);
- $w_str="";
- $f="";
- switch($p)
- {
- case 0:
- $f=" =";
- break;
- case 1:
- $f=">";
- break;
- case -1:
- $f="<";
- break;
- }
- if(!empty($f) )
- {
- foreach($w as $key=>$vaule)
- {
- if(!is_numeric($key))
- {
- if(empty($v_str))
- {
- $w_str=htmlspecialchars($key ).$f.htmlspecialchars($vaule)."'";
- }else
- {
- $w_str=$w_str.",".htmlspecialchars($key).$f.htmlspecialchars($vaule)."'";
- }
- }
- }
- }
- $str="DELETE FROM ".$t." WHERE ".$w_str;
- return $result = mysql_query($str);
- }
- function Add($t,$v)
- {
- $this->sub_sql($t);
- $k_str="";
- $v_str="";
- foreach($v as $key=>$vaule)
- {
- if(!is_numeric($key)){
- if(empty($k_str))
- {
- $k_str=htmlspecialchars($key);
- $v_str="'".htmlspecialchars($vaule)."'";
- }else
- {
- $k_str=$k_str.",".htmlspecialchars($key);
- $v_str=$v_str.","."'".htmlspecialchars($vaule)."'";
- }
- }
- }
- $str="INSERT INTO ".$t."(".$k_str.")"."value(".$v_str.")";
- return $result = mysql_query($str);
- }
- }
- ?>
-
复制代码
|