MySQL database operations like ASP

Source: Internet
Author: User

Code: [copy to clipboard] <? PHP
Class mysqldb
{
// MySQL database operations
// Prepared by: Xiong Yi
// Version: 2.0 (Release)

// Can freely reprint, modify please notify me scxy78@yeah.net
// Reprinted please keep the above statement

//, You can also specify a special table for each operation.
// Nerr indicates whether an operation error occurs. serr records the last error.CodeRecords the specific function-caused errors.
// Correct the error
// Welcome to exchange programming experience with me: scxy78@yeah.net
// My csdn: user number: scxy; what is it: bear, please take care of it more

// Can freely reprint, modify please notify me scxy78@yeah.net
// Reprinted please keep 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 selected by the database
VaR $ stname; // specify the database table for the current operation
VaR $ serr; // error code
VaR $ nerr; // indicates whether an error exists, 0 indicates no error, and 1 indicates an error.
VaR $ nresult; // query result value
VaR $ afname; // Save the array of fieldsname
VaR $ nrows; // number of rows in the query result
VaR $ ncols; // Number of columns in the query result
VaR $ anew; // Add the data after the addnew function and save it as an array
VaR $ newedit; // determines whether or not to add the file. The value 0 indicates no, the value 1 indicates addition, and the value 2 indicates editing.
VaR $ seditcon; // specify the conditions for editing records
VaR $ noffset; // record offset
VaR $ EOF; // flag whether it is at the end of the record set
VaR $ ssql; // The Last SQL statement executed

// Global variables used for update execution
VaR $ sname; // field name
VaR $ svalue; // used when the field value is addnew
VaR $ sedit; // used for field value Edit

function initialize ()
{< br> $ 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 );
}< br> function mysqldb ($ tablename = "", $ database = "SLT ") // constructor
{< br> $ this-> initialize ();
$ this-> stname = $ tablename;
$ this-> linkid = mysql_connect ($ host, $ user, $ password);
If (! $ This-> linkid)
{< br> $ this-> nerr = 1;
$ this-> serr = "mysqldb: database connection error, please start the service! ";
return;
}< br> $ this-> dbid = mysql_select_db ($ database);
If (! $ This-> dbid)
{< br> $ this-> nerr = 1;
$ this-> serr = "mysqldb: Selected Database ". $ database. "does not exist! ";
return;
}< BR >}

Function isempty ($ value)
{
If (is_string ($ value) & empty ($ value ))
Return true;
Return false;
}

Function destroy () // clear data
{
Mysql_query ("commit ");
Mysql_close ();
}

Function printerr ()
{
If ($ this-> nerr = 1)
{
Echo ($ this-> serr. "<br> ");
}
Else
{
Echo ("no error <br> ");
}
}

Function execute ($ SQL) // directly execute the SQL statement
{
If (empty ($ SQL ))
{
$ This-> nerr = 1;
$ This-> serr = "execute: The execution statement cannot be blank! ";
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 the query in the 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: it has been moved to the end of the record set! ";
Return;
}
$ This-> noffset ++;
If ($ this-> noffset >=$ this-> nrows)
$ This-> EOF = true;
}

Function moveTo ($ offset)
{
If (empty ($ offset ))
{
$ This-> nerr = 1;
$ This-> serr = "moveTo: the offset must be specified! ";
Return;
}

If (! $ This-> nresult)
{
$ This-> nerr = 1;
$ This-> serr = "moveTo: Run query first ";
Return;
}
$ This-> noffset = $ offset;
}

// Get the value of the specified column of the specified row, and return a string
// If no offset is specified, the value of the next row is obtained.
// If nfields is not specified, the value of the row is obtained and returned in array format.
Function getvalue ($ nfields =-1, $ offset =-1)
{
If ($ this-> nresult =-1)
{
$ This-> nerr = 1;
$ This-> serr = "getvalue: run 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 be reached! ";
Return;
}
}
If (! @ Mysql_data_seek ($ this-> nresult, $ this-> noffset ))
{
$ This-> nerr = 1;
$ This-> serr = "getvalue: records that do not exist in the request! ";
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. Check it carefully! ";
Return;
}
Return $ aresult [$ I];
}
Return $ aresult;
}

Function addnew ($ tablename = "") // sign to 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 to be added is empty. You can specify it during construction or 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> 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 a 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 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;
$ This-> serr = "Edit: SQL statement:". strsql. "<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) // specifies the data, which is executed after addnew;
{
If ($ this-> newedit = 0)
{
$ This-> nerr = 1;
$ This-> serr = "setvalue: Run addnew () or edit () first ()! ";
Return;
}
If (is_int ($ index ))
{
If ($ index <0 | $ index> $ this-> ncols)
{
$ This-> nerr = 1;
$ This-> serr = "setvalue: inserts 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: inserts 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 current field type
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: The field name is ". $ this-> afname [$ tmpin]. ". $ FTYPE. "The current version of the type is not supported. Please use another method to add data! ";
Return;
}

If ($ this-> newedit = 2)
$ This-> sname. = "=". $ this-> sedit;
}

Function Update () // store new values to the database
{
$ Strsql = "";

If ($ this-> newedit = 0)
{
$ This-> nerr = 1;
$ This-> serr = "Update: Execute addnew () or edit () first, and then use setvalue () to add a value! ";
Return;
}

If (empty ($ this-> svalue ))
{
$ This-> nerr = 1;
$ This-> serr = "Update: if the data is empty, you cannot add or modify the data! ";
Return;
}

Switch ($ this-> newedit)
{
Case 1: // Add
$ Strsql = "insert ";
$ 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 () SQL statement generation error. Please check! ";
Return;
}

$ This-> ssql = $ strsql;
If (! $ This-> nresult = mysql_query ($ strsql ))
{
$ This-> nerr = 1;
$ This-> serr = "Update: SQL statement:". $ strsql. "<br> MySQL error:". mysql_error ();
Return;
}
// Echo ($ this-> ssql. "<br> ");
// Cleanup
$ 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.