• notice
  • Congratulations on the launch of the Sought Tech site

Mysql operation class (packaged)

class MySQLDB
{
//MYSQL database operation class
//Author: Xiong Yi
//Version: 2.0 (release version)
//Free to reprint, please modify Notify me [email protected]
//Please keep the above statement for reprinting
//Instructions for use:
//This class is completely written in accordance with ADO's habits.People who have used ASP feel that ASP connects to the database Better than PHP (this is my feeling),
//but PHP has to be written one by one API, which is quite tiring, this class is completely encapsulated
//You can specify when creating an instance of the class A database table and the selected database, such as: new MySQLDB("table","database");
//When querying data, you can use GetValue to get the corresponding value after Query, which can be either the field name or 0 The starting serial number
//Insert a new value, first use AddNew and then use SetValue with the corresponding field name or serial number and field value, and use Update to add
//When editing, use Edit to specify the conditions for editing records.When using SetValue, Finally, use Update to add
//During the use of the class, sTName records the name of the database table used last, which can be used directly after being specified, and subsequent operations will be performed on this table by default.You can specify a special table for operation each time
//nErr indicates whether there is an error in the operation, sErr records the error code of the last error, and records which function caused the error
//Please correct the error
br>//Welcome to write to me to exchange programming experience: [email protected]
//My CSDN: User ID: scxy; nickname: Little Bear, please take care
//You can reprint freely, please modify Notify me [email protected]
//Please keep the above statement for reprinting
var $host="localhost";//host name
var $user="boot";//User name
var $password="oaserver";//User password
var $linkid;//Connection value
var $dbid;//Result of database selection Value
var $sTName;//Specify the database table of the current operation
var $sErr;//Error code
var $nErr;//Indicate whether there is an error, 0 no error, 1 error
var $nResult;//Query result value
var $aFName;//Array to save FieldsName
var $nRows;//Number of rows in query result
var $nCols;//Query result The number of columns in
var $aNew;//The data added after the AddNew function is saved in the form of an array
var $NewEdit;//Determine whether the addition operation is currently in progress, 0 means no, 1 means in progress Add, 2 means edit
var $sEditCon;//Specify the conditions for editing records
var $nOffset;//Record offset
var $EOF;//Mark whether to end of recordset
var $sSQL;//The last executed SQL statement
//Global variables used to execute Update
var $sName;//Field name
var $sValue;//Use
var $sEdit when field value AddNew;//Use
function Initialize() when field value Edit
{
$this->nErr=0;
$this-> NewEdit=0;
$this->nResult=-1;
$this->nCols=0;
$this->nRows=0;
$this->nOffset=0;
$this->EOF=true;
$this->sName="";
$this->sValue="#@!";
$this->sEdit="#@!";
unset($this->aFName);
unset($this->aNew);
}
function MySqlDB($TableName=" ",$database="slt")//Constructor
{
$this->Initialize();
$this->sTName=$TableName;
$this-> linkid=mysql_connect($host,$user,$password);
if(!$this->linkid)
{
$this->nErr=1;
$this->sErr="MySqlDB: database connection error, please start the service!";
return;
}
$this->dbid=mysql_select_db($database);
if(!$this->dbid)
{
$this->nErr=1;
$this->sErr="MySqlDB:Selected database ".$database." does not exist!";
return;
}
}
function IsEmpty($Value)
{
if(is_string($Value)&&empty($Value))
return true;
return false;
}
function Destroy ()//Data clear processing
{
mysql_query("commit");
mysql_close();
}
function PrintErr()
{
if($this->nErr==1)
{
echo($this->sErr."<br><br>");
}
else
{
echo("No error<br><br>");
}
}
function Execute($SQL)//Execute the SQL statement directly
{
if(empty ($SQL))
{
$this->nErr=1;
$this->sErr="Execute: Execute statement cannot be empty! ";
return false;
}
$this->sSQL=$SQL;
if(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL statement: ".$SQL."<br>MySql error: ".mysql_error();
return false;
}
return true;
}
function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="")//In the database Execute the query
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$strSQL=" select ".$SQL." from ".$this->sTName;
if(!empty($Condition))
$strSQL=$strSQL." where ".$Condition;
if(!empty($Order))
$strSQL=$strSQL." order by ".$Order;
if(!empty($Sequenc))
$strSQL=$strSQL." ".$Sequenc;
$this->sSQL=$strSQL;
if(!$this-> nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Query:SQL statement: ".$strSQL."<br> MySql error: ".mysql_error()."<br>";
return;
}
$this->nOffset=0;
$this->nRows=mysql_num_rows($ this->nResult);
$this->nCols=mysql_num_fields($this->nResult);
if($this->nRows>0)
$this->EOF=false;
else
$this->EOF=true;
unset($this->aFName);
$this->aFName=array();
for ($i=0;$i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function MoveNext()
{
if($this->EOF)
{
$this->nErr=1;
$this->sErr="MoveNext: Moved to the end of the recordset! ";
return;
}
$this->nOffset++;
if($this->nOffset>=$this->nRows)
$this->EOF=true;
}
function MoveTo($Offset)
{
if(empty($Offset))
{
$this->nErr=1;
$this->sErr="MoveTo: Offset must be specified! ";
return;
}
if(!$this->nResult)
{
$this->nErr=1;
$this->sErr="MoveTo: Please execute the query first: Query";
return;
}
$this->nOffset=$Offset;
}
//Get the value of the specified column of the specified row and return a string
//If Offset is not specified, the value of the next row will be obtained
//If nFields is not specified, the value of the row will be obtained , and returned as an array
function GetValue($nFields=-1,$Offset=-1)
{
if($this->nResult==-1)
{
$this->nErr=1;
$this->sErr="GetValue: Please execute the Query() function first! ";
return;
}
if($Offset>-1)
{
$this->nOffset=$Offset;
if($this->nOffset> ;=$this->nRows)
{
$this->nErr=1;
$this->sErr="GetValue: The requested offset is too large to be reached! ";
return;
}
}
if([email protected]_data_seek($this->nResult,$this->nOffset))
{
$this-> ;nErr=1;
$this->sErr="GetValue: Requesting a record that does not exist! ";
return;
}
$aResult=mysql_fetch_row($this->nResult);
if(is_int($nFields)&&$nFields>-1)
{
if($nFileds>$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:Requested column value Greater than the actual column value! ";
return;
}
return $aResult[$nFields];
}
if(is_string($nFields))
{
$nFields=strtolower($ nFields);
for($i=0;$i<$this->nCols;$i++)
{
if($this->aFName[$i]==$nFields)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue: The requested column does not exist, please double check! ";
return;
}
return $aResult[$i];
}
return $aResult;
}
function AddNew($TableName="")//flag start adding data
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
if ($this->NewEdit>0)
{
$this->nErr=1;
$this->sErr="AddNew: You are adding or updating the database! ";
return;
}
if(empty($this->sTName))
{
$this->nErr=1;
$this-> sErr="AddNew: The database table you want to add is empty, it can be specified during construction, or it can be specified during AddNew()! ";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=1;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="AddNew:SQL statement: ".strSQL."<br><br>MySql error: ".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function Edit($Condition="",$TableName="")//Edit the specified database table
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$this->sEditCon=$Condition;
if(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="Edit: Please specify the database table before editing !";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=2 ;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$t his->sErr="Edit:SQL statement: ".strSQL."<br><br>MySql error: ".mysql_error();
return;
}
$this-> ;nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0; $i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}
function SetValue($Index,$Value)//Specify data, execute after AddNew;
{
if($this->NewEdit==0)
{
$this-> ;nErr=1;
$this->sErr="SetValue: Please execute AddNew() or Edit() first! ";
return;
}
if(is_int($Index))
{
if($Index<0||$Index>$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue: Insert non-existent column value! ";
return;
}
$this->aNew[$Index]=$Value;
$tmpIn=$Index;
}
elseif(is_string($Index))
{
$Index=strtolower($Index);
for($i=0;$i<$this->nCols;$i++)
{
if($this->aFName[$i]==$Index)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue: Insert non-existent column value! ";
return;
}
$this->aNew[$i]=$Value;
$tmpIn=$i;
}
if(!empty($ this->sName))
$this->sName.=",";
$this->sName.=$this->aFName[$tmpIn];
//According to The type of the current field generates the corresponding new value
if($this->sValue!="#@!")
$this->sValue.=",";
else
$this->sValue="";
[email protected]_field_type($this->nResult,$i);
//echo($ftype.",".$this->aNew [$i].",".$i.":".$sValue."<br>");
switch($ftype)
{
case "string":
case "date":
case "datetime":
$this->sValue.=""".$this->aNew[$tmpIn].""";
$this->sEdit=""".$this->aNew[$tmpIn].""";
break;
case "int":
case "unknown":
$this->sValue.=$this->aNew[$tmpIn];
$this->sEdit=$this->aNew[$tmpIn];
break;
default:
$this->nErr=1;
$this->sErr="Update: ".$ftype." type with field name ".$this->aFName[$tmpIn]." The current version does not support, please use another method to add data! ";
return;
}
if($this->NewEdit==2)
$this->sName.="=".$this->sEdit;
}
function Update()//Store new value to database
{
$strSQL="";
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="Update: Please execute AddNew() or Edit() first, and then use SetValue() to add the value! ";
return;
}
if(empty($this->sValue))
{
$this->nErr=1;
$this-> sErr="Update: When the data is empty, data cannot be added or modified! ";
return;
}
switch($this->NewEdit)
{
case 1://Add
$strSQL="insert into ";
$strSQL.=$this->sTName;
$strSQL.=" (".$this->sName.") ";
$strSQL.="values ​​(".$this->sValue.")";
break;
case 2://Modify
$strSQL="update ";
$strSQL.=$this->sTName;
$strSQL.=" set ";
$strSQL.=$this->sName;
if(!empty($this->sEditCon))
$strSQL.=" where ".$this->sEditCon;
break;
default:
$this->nErr=1;
$this->sErr="Update:Update() generates SQL statement error, please check!";
return;
}
$ this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Update:SQL statement: ".$strSQL."<br><br>MySql error: ".mysql_error();
return;
}
//echo($ this->sSQL."<br>");
//Clean up
$this->NewEdit=0;
uns et($this->aNew);
mysql_query("commit");
}
}

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+