One database operation class
[PHP] code
Persistency = $ persistency; $ this-> user = $ sqluser; $ this-> password = $ sqlpassword; $ this-> server = $ sqlserver; $ this-> dbname = $ database; if ($ this-> persistency) {$ this-> db_connect_id = mysql_pconnect ($ this-> server, $ this-> user, $ this-> password);} else {$ this-> db_connect_id = mysql_connect ($ this-> server, $ this-> user, $ this-> password );} if ($ this-> db_connect_id) {if ($ database! = "") {$ This-> dbname = $ database; $ dbselect = mysql_select_db ($ this-> dbname); if (! $ Dbselect) {mysql_close ($ this-> db_connect_id); $ this-> db_connect_id = $ dbselect ;}return $ this-> db_connect_id;} else {return false ;}} // Other basic methods function SQL _close () // Close the database {if ($ this-> db_connect_id) {if ($ this-> query_result) {// mysql_free_result () you only need to call it when considering how much memory will be occupied when a large result set is returned. // After the script ends, all associated memory will be automatically released. Mysql_free_result ($ this-> query_result);} // Close the database $ result = mysql_close ($ this-> db_connect_id); return $ result;} else {return false ;}} // SQL statement execution function SQL _query ($ query = "", $ transaction = FALSE) {// release memory unset ($ this-> query_result); if ($ query! = "") {$ This-> num_queries ++; $ this-> query_result = mysql_query ($ query, $ this-> db_connect_id);} if ($ this-> query_result) {// if the query result exists, the memory unset ($ this-> row [$ this-> query_result]) will be released; unset ($ this-> rowset [$ this-> query_result]); return $ this-> query_result;} else {return ($ transaction = END_TRANSACTION )? True: false;} // defines the number of rows to obtain data. function SQL _numrows ($ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {$ result = mysql_num_rows ($ query_id); return $ result ;} else {return false ;}/// obtain the number of record rows affected by the previous MySQL operation function SQL _affectedrows () {if ($ this-> db_connect_id) {$ result = mysql_affected_rows ($ this-> db_connect_id); return $ result;} else {return false ;}} // Obtain the number of fields in the result set function SQL _numfields ($ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {$ result = mysql_num_fields ($ query_id); return $ result ;} else {return false ;}/// obtain the field name of the specified field in the result. function SQL _fieldname ($ offset, $ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {$ result = mysql_field_name ($ query_id, $ offset); return $ result ;} else {return false ;}// function SQL _fieldtype ($ offset, $ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {$ result = mysql_field_type ($ query_id, $ offset); return $ result ;} else {return false ;}} function SQL _fetchrow ($ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {$ this-> row [$ query_id] = mysql_fetch_array ($ query_id ); return $ this-> row [$ query_id];} else {return false;} function SQL _fetchrowset ($ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {unset ($ this-> rowset [$ query_id]); unset ($ this-> row [$ query_id]); while ($ this-> rowset [$ query_id] = @ mysql_fetch_array ($ query_id )) {$ result [] = $ this-> rowset [$ query_id];} return $ result;} else {return false;} function SQL _fetchfield ($ field, $ rownum =-1, $ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {if ($ rownum>-1) {// value of the obtained result $ result = @ mysql_result ($ query_id, $ rownum, $ field);} else {if (empty ($ this-> row [$ query_id]) & amp; empty ($ this-> rowset [$ query_id]) {if ($ this-> SQL _fetchrow ()) {$ result = $ this-> row [$ query_id] [$ field] ;}} else {if ($ this-> rowset [$ query_id]) {$ result = $ this-> rowset [$ query_id] [0] [$ field];} else if ($ this-> row [$ query_id]) {$ result = $ t His-> row [$ query_id] [$ field] ;}}return $ result ;}else {return false ;}// select function SQL _rowseek ($ rownum, $ query_id = 0) {if (! $ Query_id) {// get the value of $ query_id = $ this-> query_result;} if ($ query_id) {// Move the internal result pointer $ result = @ mysql_data_seek ($ query_id, $ rownum); return $ result;} else {return false;} function SQL _nextid () {if ($ this-> db_connect_id) {// obtain the ID $ result = @ mysql_insert_id ($ this-> db_connect_id) generated by the previous INSERT operation; return $ result ;} else {return false ;}} function SQL _freeresult ($ query_id = 0) {if (! $ Query_id) {$ query_id = $ this-> query_result;} if ($ query_id) {// release the memory unset for the given variable ($ this-> row [$ query_id]); unset ($ this-> rowset [$ query_id]); // release the memory mysql_free_result ($ query_id); return true;} else {return false ;}// function SQL _error ($ query_id = 0) with an error in SQL statement execution) {$ result ["message"] = mysql_error ($ this-> db_connect_id); $ result ["code"] = mysql_errno ($ this-> db_connect_id ); return $ result ;}}?>