MySQL Action class (encapsulated)

Source: Internet
Author: User
Tags commit error code table name mysql database
Class MySQLdb
{
MySQL Database operations class
Author: Xiang
Version: 2.0 (release)
Be free to reprint, amend please inform me scxy78@yeah.net
Reprint please retain the above statement
Instructions for use:
This class is written completely according to the custom of ADO, the people who use ASP feel that ASP connection database is more useful than PHP (this is my feeling),
But PHP has an API to write, very tired, the class did a complete encapsulation
When you create an instance of a class, you can specify a database table and a selected database, such as: New MySQLdb ("table", "database");
Query data, you can use GetValue to get the corresponding value, can be a field name can also be the beginning of 0 ordinal
Inserts a new value, first uses the AddNew after using SetValue the corresponding field name or the ordinal and the field value, in uses the update to add
Use edit to specify the criteria for editing records using SetValue, and then add with update
During class use, the Stname records the last used database table name, which can be used directly when specified, and the subsequent action defaults to the table
, and you can, of course, specify a particular table at a time.
Nerr indicates whether an action error occurred, serr the error code for the last error, and recorded a specific error caused by which function
Please correct me where the mistake is.
Welcome letter and I exchange programming experience: Scxy78@yeah.net
My csdn: User number: Scxy; say: Bear, please take care of
Be free to reprint, amend please inform me scxy78@yeah.net
Reprint please retain the above statement
var $host = "localhost"; Host Name
var $user = "Boot"; User name
var $password = "Oaserver"; User password
var $linkid; Connection value
var $dbid; The result value of the database selection
var $sTName; Specify the database table for the current operation
var $sErr; Error code
var $nErr; Indicates if there is an error, 0 error free, 1 error
var $nResult; Query result value
var $aFName; Save an array of Fieldsname
var $nRows; Number of rows in query results
var $nCols; Number of columns in query results
var $aNew; The data added after the AddNew function is saved as an array
var $NewEdit; Determines whether an add operation is currently in progress, 0 indicates no, 1 is in the Add, and 2 represents the edit
var $sEditCon; Specify criteria for editing records
var $nOffset; Record offset
var $EOF; Mark whether to the end of the recordset
var $sSQL; The last SQL statement executed
Global variables to be used to perform the update
var $sName; Field name
var $sValue; Field value is AddNew with
var $sEdit; Field value edit is used
function Initialize ()
{
$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 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 cleanup 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 SQL statements directly
{
if (empty ($SQL))
{
$this->nerr=1;
$this->serr= execute: Execution 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 = "")//Execute query in database
{
$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 query first:";
Return
}
$this->noffset= $Offset;
}
Gets the value of the specified column in the specified row, and returns a string
If you do not specify offset, the value of the next row is obtained
If Nfields is not specified, the value of the row is taken and the array form returns
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 required offset is too large to achieve!" ";
Return
}
}
if (! @mysql_data_seek ($this->nresult, $this->noffset))
{
$this->nerr=1;
$this->serr= "GetValue: Request 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: The requested column value is 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 check it carefully!" ";
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, can be specified at construction time, or specified at 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: Specify a 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;
$this->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, followed by AddNew execution;
{
if ($this->newedit==0)
{
$this->nerr=1;
$this->serr= "SetValue: Please perform AddNew () or edit () first! ";
Return
}
if (Is_int ($Index))
{
if ($Index <0| | $Index > $this->ncols)
{
$this->nerr=1;
$this->serr= "SetValue: Inserting a column value that does not exist!" ";
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: Inserting a column value that does not exist!" ";
Return
}
$this->anew[$i]= $Value;
$tmpIn = $i;
}
if (!empty ($this->sname))
$this->sname.= ",";
$this->sname.= $this->afname[$tmpIn];
Generate a new value based on the type of the current field
if ($this->svalue!= "#@!")
$this->svalue.= ",";
Else
$this->svalue= "";
$ftype = @mysql_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: Field name". $this->afname[$tmpIn]. " ". $ftype." Type is not supported in the current version, please add the data in another way! ";
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 perform AddNew () or edit (), then add SetValue () value! ";
Return
}
if (Empty ($this->svalue))
{
$this->nerr=1;
$this->serr= Update: You cannot add or modify data when the data is empty! ";
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 () error generating SQL statement, 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> ");
For clean-up work.
$this->newedit=0;
unset ($this->anew);
mysql_query ("commit");
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.