MySQL operation class (new version of the revised version)

zhaozj2021-02-17  70

class MySQLDB {// MYSQL database operations // Author: Xiong Yi // Version: 2.0 (release) // free to reprint, modify Notify me scxy78@yeah.net // reproduced Please keep the above statement // Description : // This class is written in accordance with ADO's habit, people who have used ASP feel that the ASP connection database is better than PHP (this is my feeling), // But PHP has a API, it is quite tired, Categories When doing a complete package // Create an instance of the class, you can specify a database table and the selected database, such as: new mysqldb ("Table", "Database"); // Query Query, you can get the corresponding Value, or both the field name or the number // plug-in value started, first use the AddNEW, the corresponding field name or sequence number or segment value and field value, use the Update to add // editing with edit specified editing record The condition is in use of SetValue, and finally add // during class usage, the STNAME records in the class usage. When specified, you can use it directly, and the next operation is performed on the table // on this table //. Of course, You can perform a special table each time // Nerr indicates whether the error code is an error, the SERR records the last error code, records which function caused by the clearness of the error 正 / 错 请 正 正 正 正 正 正 正 正 正 正 正Programming experience: scxy78@yeah.net // My 9CBS: user number: scxy; said: Bear, please take care of // You can freely reprint, please inform 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; // Database Selection Result VAR $ STNAME; / / Specify the currently operated database table var $ serr; // Error code var $ nerr; / / indicator whether there is an error existing, 0 no error, 1 has error VAR $ nresult; // Query Result Value VAR $ AFNAME; / / Save FieldSName's Ari Var $ NROWS; / / The number of rows in the query VAR $ ncols; // The number of columns in the query value var $ ANEW; // Add data after addNew function In the form of an array, VAR $ NEWEDIT is saved. / / Decorate if the current add operation, 0 means not, 1 means adding, 2 means editing var $ SEDITCON; // Specify the condition VAR $ noffset; // Record Offset VAR $ EOF; // Tags Whether to log interaction VAR $ SSQL; // Perform Update's global variable VAR $ SNAME; // field name Var $ sValue; // / Field value addNew with var $ sedit; // field value Edit

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 the service! "; Return;} $ this-> dbid = mysql_select_db ($ database); if (! $ This-> dbid) {$ this-> Nerr = 1; $ this-> serr =" mysqldb: selection Database. "$ Database." Does not exist! "; Return;}}}} {if (is_string ($ value) && empty ($ value) Return true;}

Function Destroy () // Data Clear Process {mysql_Query ("Commit"); mysql_close ();}

function PrintErr () {if ($ this-> nErr == 1) {echo ($ this-> sErr "

".);} else {echo ( "no error

" }}

Function Execute // Directly performs SQL statement {if (Empty ($ SQL)) {$ this-> Nerr = 1; $ this-> serr = "Execute: Execute statement can not be empty!"; Return False; } $ This-> ssql = $ sql; if (! MySQL_QUERY ($ SQL)) {$ this-> Nerr = 1; $ this-> serr = "EXECUTE: SQL statement:". $ Sql. "
MySQL Error: ". MySQL_ERROR (); return false;} Return true;

Function Query ($ TABLENAME = ", $ SQL =" * ", $ condition =", $ order = ", $ sequence =" ") // execute queries in the database {$ this-> initialize (); IF (! EMPTY ($ TABLENAME)) $ this-> stname = $ TABLENAME; $ strsql = "select". $ sql. "from". $ this-> stname; if (! EMPTY ($ condition) $ strsql = $ strsql. "Where"; 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. "
MySQL error:". MySQL_ERROR (). "
"; 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 <$ = ncols; $ i ) $ this-> afname [$ I] = STRTOL Ower ($ this-> nresult, $ i);

Function MoveNext () {if ($ this-> EOF) {$ this-> Nerr = 1; $ this-> serr = "MOVENEXT: Has been moved to the end of the recordset!"; return;} $ this-> Noffset ; if ($ this-> noffset> = $ this-> nrows) $ this-> EOF = true;} Function Moveto ($ OFFSET) {IF ($ this-> Nerr = 1; $ THIS- > SERR = "Moveto: You must specify an offset!"; return;}

IF ($ this-> nresult) {$ this-> Nerr = 1; $ this-> serr = "Moveto: please check: query"; return;} $ this-> noffset = $ OFFSET;}

/ / Get the value of the specified column of the specified line, return to the string // If the OFFSET will acquire the value of the next row // If you do not specify the value of the NFields will get the value of the row, return FUNCTION GETVALUE in the array ($ nfields = -1, $ OFFSET = -1) {IF ($ this-> nresult == - 1) {$ this-> Nerr = 1; $ this-> serr = "GetValue: Please perform the query () function!"; Return;}}}} ($ >-> noffset = $ offet; if ($ this-> noffset> = $ this-> nrows) {$ this-> Nerr = 1; $ this-> Serr = "GetValue: The required offset is too large, not achievable!"; Return;}} if (! @ Mysql_data_seek ($ this-> nresult, $ this-> noffset)) {$ this-> Nerr = 1; $ this-> serr = "GetValue: Request does not exist!"; return;} $ ARESULT = mysql_fetch_row ($ this-> nresult); if (is_int ($ nfields) && $ nfields> -1) {ix ($ Nfileds> $ this-> ncols) {$ this-> Nerr = 1; $ this-> serr = "GetValue: The requested column value is greater than the actual column value!"; return;} returnid $ ARESULT [$ nfields]; } If (is_string ($ nfields)) { $ nfields = strtolower ($ nfields); for ($ I = 0; $ I <$-> ncols; $ i ) {if ($ this-> afname [$ I] == $ nfields) Break;} $ i == $ this-> ncols) {$ this-> Nerr = 1; $ this-> serr = "getValue: The requested column does not exist, please check carefully! "}}}} Return $ ARESULT [$ I];

} Return $ ARESULT;} Function AddNew ($ TABLENAME = ") // Sign Start Add Data {$ this-> Initialize (); if (! EMPTY ($ TABLENAME)) $ this-> stname = $ TABLENAME; IF $ this-> NEWEDIT> 0) {$ this-> Nerr = 1; $ this-> serr = "Add: You are adding or updating the database!"; return;} if (Empty ($ this-> stname) )) {$ This-> Nerr = 1; $ this-> serr = "AddNew: The database table wants to add is empty, you can specify in the configuration, or you can specify when addnew ()!"; Return;} unset ($ this-> anew); $ this-> anew = array (); $ this-> newitedit = 1; $ strsql = "select * from". $ this-> stname; $ this-> ssql = $ strsql; if {$ this-> nErr = 1; $ this-> sErr = ($ this-> nResult = mysql_query ($ strSQL)!).. "AddNew: SQL statement:" strSQL "

MySql error: ".MYSQL_ERROR (); return;} $ this-> ncols = mysql_num_fields ($ this-> nresult); unset ($ this-> afname); $ this-> afname = array (); for ($ i = 0; $ I <$-> ncols; $ i ) $ this-> afname [$ I] = strtolower ($ this-> nresult, $ i);}

Function Edit ($ CONDITION = ", $ TABLENAME =" ") // Edit the specified database table {$ this-> Initialize (); if (! Empty ($ TABLENAME)) $ this-> stname = $ TABLENAME; $ this-> Seditcon = $ condition; if ($ 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-> nErr = 1; $ this-> sErr = ($ this-> nResult = mysql_query ($ strSQL)!). "Edit: SQL statement:" strSQL "

. 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] = start_name ($ this-> nresult, $ i);}

Function SetValue / / Specify the data, followed by AddNew; {if ($ this-> newedit == 0) {$ this-> Nerr = 1; $ this-> serr = setValue: Please perform addNew () or edit ()! "; Return;} if (is_int ($ index)) {if ($ index <0 | $ index> $ this-> ncols) {$ this-> Nerr = 1 $ This-> serr = "setValue: Insert the column value!"; Return;} $ this-> anew [$ index] = value; $ tmpin = $ index;} elseif (is_string ($ 有DEX)) {$ INDEX = STRTOLOWER ($ INDEX); for ($ I = 0; $ I-> ncols; $ i ) {IF ($ this-> afname [$ I] == $ index) Break;} if ($ I == $ this-> ncols) {$ this-> Nerr = 1; $ this-> serr = "SetValue: Insert the column value of the existing!"; } $ This-> anew [$ I] = $ value; $ tmpin = $ i;} if (! Empty ($ this-> sname)) $ this-> sname. = "; $ This-> sname. = $ THIS-> AFNAME [$ Tmpin]; // Generate the corresponding new value IF according to the current field type ($ this-> svalue! = "# @!") $ this-> svalue. = ","; else $ this-> svalue = ""; $ ftype = @ mysql_field_type ($ this-> NResult, $ i);

//echo ($$ ""); "<"); switch ($ ftype) {casse "); 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 current version does not support, please add data with other methods!"; return;} if ($ this-> newedit == 2) $ this-> sname. = "=". $ this-> Sedit;

Function update () // stores new value to database {$ strsql = ""

IF ($ this-> NEWEDIT == 0) {$ this-> Nerr = 1; $ this-> serr = "Update: Please perform addNew () or edit (), then use setValue () Add value!"; Return;}

IF ($ this-> svalue) {$ this-> nerr = 1; $ this-> serr = "Update: In the case of empty, you cannot add or modify the data!"; Return;}

Switch ($ this-> newedit) {Case 1: // Add $ strsql = "insert Into"; $ strsql. = $ strsql. = "(") " $ 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 a SQL statement error, please check!"; Return;} $ this-> ssql = $ strsql; if (! $ This-> nresult = mysql_query ($ strsql)) {$ this-> nErr = 1; $ this-> sErr = "Update: SQL statement:". $ strSQL "

MySql error:" mysql_error (); return;..} // echo ($ This-> ssql. "
"); // Clean up $ this-> newited = 0; unset ($ this-> anew); mysql_query ("commit");}}?>

转载请注明原文地址:https://www.9cbs.com/read-31389.html

New Post(0)