PHP's MySQL operation class, using the function on the manual, but it is more convenient. Many said no, I will know.

xiaoxiao2021-03-06  70

Var $ dbhost = ''; / * Database host name * / var $ dbuser = ''; / * Database User Name * / var $ dbpswd = ''; / * Database Password * / var $ dbname = '; / * Name database */

Var $ Result = null; / * private * / var $ linkid = null; / * private * / var $ record = null; / * contains a record * / var $ rows = 0;

Var $ mustbehalt = true; / * Have an error Stop * / var $ recordcase = null; / * Only appears in the next NEXTRECORD () and F () method * /

Function DummyMysqlclass ($ dbhost = ', $ dbuser =', $ dbpswd = ', $ dbname =', $ newlink = false) {if (is_ARRAY ($ dbhost)) {$ dbhost = $ dbhost [0] ; $ Dbuser = $ dbhost [1]; $ dbpswd = $ dbhost [2]; $ dbname = $ dbhost [3]; $ newlink = Empty ($ dbhost [4])? False: $ dbhost [4];} elseif (str_replace (array ("/", "@"), array ("," "), $ dbhost)! = $ dbhost) {$ dsn = $ this-> splitdsn ($ dbhost);

$ Dbtpye = $ dsn [0]; $ dbhost = $ dsn [1]. ":" $ Dsn [2]; $ dbuser = $ dsn [3]; $ dbpswd = $ dsn [4]; $ dbname = $ DSN [5]; $ newlink = $ dsn [6] == '? $ newlink: $ dsn [6];} $ this-> dbhost = Empty ($ dbhost)? $ this-> dbhost: $ dbhost; $ This-> dbuser = Empty ($ dbuser)? $ this-> dbuser: $ dbuser; $ this-> dbpswd = empty ($ dbpswd) $ this-> dbpswd: $ dbpswd; $ this-> dbname = Empty ($ DBNAME)? $ This-> DBNAME: $ dbname;

If (! Empty ($ dbhost)) {$ this-> Connect ($ this-> dbhost, $ this-> dbuser, $ this-> dbpswd, $ newlink);} if (! Empty ($ dbname) {$ this-> selectdb ($ dbname);}}

Function Splitdsn ($ DSN) {// $ dsn = "Database Type Name: // Entrance account: Entrance Password @ Database Host Name: Port number / database name / is always open new connection"; // $ dsn = " MySQL: // Dummy: 123 @ localhost: 3306 / TETX / FALSE "; $ dsn = preg_split (" / [: @ @】 / ", $ dsn); $ dbtpye = '; $ dbhost ='; $ DBPORT = ''; $ dbuser = ''; $ dbpswd = ''; $ dbname = ''; $ dbnewlink = false;

$ Dbtpye = $ dsn [0]; $ dbhost = $ dsn [5]; $ dbport = $ dsn [6]; $ dbuser = $ dsn [3]; $ dbpswd = $ dsn [4]; $ dbname = $ dsn [7]; $ dbnewlink = $ dsn [8];

Return Array ($ DBTPYE, $ DBHOST, $ DBPORT, $ DBUSER, $ dbpswd, $ dbname, $ dbnewlink);

Function AffectedRows () {/ * Number of records that get the previous MySQL operation * / return mysql_affected_rows ($ this-> linkid);}

Function Changeuser ($ User, $ Password) {/ * Change the user in the active connection * / return mysql_change_user ($ User, $ Password, $ this-> dbname, $ this-> linkid);}

Function cliencoding () {/ * Returns the name of the character set * / return mysql_client_encoding ($ this-> linkid);

Function close () {/ * Close mysql connection * / $ close = mysql_close ($ this-> linkid); $ this-> linkid = null; $ this-> result = null; $ this-> record = null; return $ CLOSE;

Function Connect ($ dbhost = ', $ dbuser =', $ dbpswd = ', $ newlink = false) {//, int client_flags) {/ * Open a connection to the MySQL server * / $ connect = @Mysql_connect (Empty ($ dbhost) $ this-> dbhost: $ dbhost, empty ($ dbuser)? $ this-> dbuser: $ dbuser, Empty ($ dbpswd)? $ this-> dbpswd: $ dbpswd, $ newlink); IF (! is_resource)) {$ this-> Halt ("Connection Database Failed!", 1); Return False;} $ this-> linkid = $ connect; Return true;}

Function CreatedB ($ DBNAME) {/ * New Mysql Database * / Return @Mysql_create_db ($ DBNAME, $ THIS-> Linkid) OR Die ($ THIS-> Halt ("Created Database". $ dbname. "Failed!") );} Function Dataseek ($ ROWNUMBER) {/ * Mobile internal results pointer * / return mysql_data_seek ($ this-> Result, $ rownumber);}

Function DBNAME ($ ROW, $ FIELD = NULL) {/ * acquisition data * / if (Empty ($ FIELD)) {Return MySQL_DB_NAME ($ this-> Result, $ row);} return mysql_db_name ($ this-> Result , $ row, $ field);

Function DBQuery ($ DBNAME, $ querystring) {/ * Send a mysql query * / $ this-> Result = mysql_db_query ($ dbname, $ querystring, $ this-> linkid); return $ this-> result? true: false; }

Function DropDB ($ DBNAME) {/ * Discard (Delete) A mysql database * / return mysql_drop_db ($ dbname, $ this-> linkid);}

Function errno () {/ * Returns the digital code of the error message in the last mysql operation * / return mysql_errno ($ this-> linkid);

Function error () {/ * Returns the text error message generated by the last MySQL operation * / return mysql_error ($ this-> linkid);

Function EscapeString {/ * escape a string for mysql_query * / return mysql_escape_string ($ unescapedstring);

Function Fetcharray ($ ROWS = 0, $ ResultType = mysql_both) {/ * gets a line from the results set as an associated array, or the number array, or both * / if (! is_resource ($ this-> result) {Return False;} $ fetcharray = mysql_fetch_array ($ this-> Result, $ resulttype); if ($ fetcharray& $ rows) {$ this-> rows ;} return $ fetcharray;}

Function Fetchassoc ($ rows = 0) {/ * gets a line from the results set as an associated array * / if (! is_resource ($ this-> result) {return false;} $ fetchassoc = mysql_fetch_assoc ($ this-> result); IF ($ FETCHASSOC && $ ROWS) {$ this-> rows ;} Return $ FETCHASSOC;

function fetchField ($ fieldOffset = NULL) {/ * From the result set containing information of the object as the return * / if (empty ($ fieldOffset)) {return mysql_fetch_field ($ this-> Result, $ fieldOffset);} return mysql_fetch_field ($ This-> result;} function fetchLength () {/ * acquisition result set per output length * / return mysql_fetch_lengths ($ this-> result);}

Function Fetchobject ($ rows = 0) {/ * gets a line from the results set as an object * / if ($ this-> result) {return false;} $ fetchobject = mysql_fetch_object ($ this-> result); if (is_Object ($ fetchobject) && $ rows) {$ this-> rows ;} return is_object ($ fetchobject)? $ fetchobject: false;}

Function Fetchrow ($ ROWS = 0) {/ * acquires a line from the result set as an enumeration number * / if (! is_resource ($ this-> result) {return false;} $ fetchrow = mysql_fetch_row ($ this-> result) ; If ($ FETCHROW && $ ROWS) {$ this-> rows ;} return $ fetchrow;}

Function FieldFlags ($ FIELDOFFSET) {/ * acquires and specifies the symbols associated with the specified field * / return mysql_field_flags ($ this-> Result, $ fieldoffset);}

Function Fieldlen ($ FIELDOFFSET) {/ * Returns the length of the specified field * / return mysql_field_len ($ this-> Result, $ fieldoffset);}

Function FieldName ($ FIELDINDEX) {/ * Field Name of the specified field in the result * / return mysql_field_name ($ this-> Result, $ FIELDEX);

Function Fieldseek ($ FIELDOFFSET) {/ * Set the pointer in the result set to the set field offset * / return mysql_field_seek ($ this-> Result, $ fieldoffset);}

Function FieldTable ($ FIELDOFFSET) {/ * get the table name where the specified field is located * / return mysql_field_table ($ this-> Result, $ fieldoffset);}

Function FieldType ($ FIELDOFFSET) {/ * acquisition result set specified field type * / return mysql_field_type ($ this-> Result, $ fieldoffset);}

Function freresult () {/ * release result memory * / return mysql_free_result ($ this-> result);}

Function getClientInfo () {/ * gets mysql client information * / return mySQL_GET_CLIENT_INFO ();

Function gethostinfo () {/ * gets mysql host information * / return mysql_get_host_info ($ this-> linkid);} Function getProtoInfo () {/ * gets mysql protocol information * / return mysql_get_proto_info ($ this-> linkid);}

Function GetServerInfo () {/ * gets mysql server information * / return mysql_get_server_info ($ this-> linkid);

Function INFO () {/ * gets the recent query information * / return mysql_info ($ this-> linkid);

Function INSERTID () {/ * gets the id * / return mysql_insert_id ($ this-> linkid) generated by the previous INSERT operation;

Function Listdbs () {/ * lists all databases in the MySQL server * / $ this-> Result = mysql_list_dbs ($ this-> linkid); return $ this-> result? true: false;}

Function Listfields ($ DBNAME, $ TABLENAME) {/ * lists the fields in mysql results * / $ this-> Result = mysql_list_fields ($ dbname, $ TABLENAME, $ THIS-> LinkID); Return $ this-> Result? True : False;}

Function ListProcesses () {/ * lists the mysql process * / $ this-> result = mysql_list_processes ($ this-> linkid); return $ this-> Result? true: false;}

Function ListTables ($ DBNAME = ') {/ * lists tables in the mysql database * / $ dbname = Empty ($ dbname)? $ this-> dbname: $ dbname; $ this-> result = mysql_list_tables ($ dbname, $ this-> linkid; return $ this-> result? true: false;}

Function Numfields () {/ * Number of Results The number of fields * / return mysql_num_fields ($ this-> result);

Function Numrows () {/ * Number of results concentration * / return mysql_num_rows ($ this-> result);

Function PConnect ($ dbhost = ', $ dbuser =', $ dbpswd = ') {/ * Open a lasting connection to the MySQL server * / $ connect = @mysql_pconnect ($ dbhost)? $ this-> DBHOST: $ DBHOST, EMPTY ($ dbuser)? $ This-> dbuser: $ dbuser, empty ($ dbpswd)? $ This-> dbpswd: $ dbpswd); if (! Is_resource ($ connect)) {$ this-> HALT ("Connection Database Failed!", 1); Return False;} $ this-> linkid = $ connect; return true;} function ping () {/ * ping a server connection, re-connect if no connection * / return MySQL_PING ($ this-> linkid);

Function Query ($ querystring) {/ * Send a mysql query * / if ($ this-> halt ("SQL statement is empty!", 1); return false;} if (! is_resource $ this-> linkid) {$ this-> Halt ("Please make sure the database is already connected!", 1); return false;} $ this-> result = mysql_query ($ querystring, $ this-> linkid); // or Die (MySQL_ERROR ()); // Print_r ($ this); Return $ this-> Result? True: false;}

Function ReaScapeString ($ unescapedstring) {/ * escapes special characters in the string used in the SQL statement, and considers the current character set * / return mySQL_REAL_ESCAPE_STRING ($ unescapestring, $ this-> linkid);

Function Result ($ ROW, $ FIELD = NULL) {/ * acquisition data * / if (Empty ($ FIELD)) {Return MySQL_Result ($ this-> Result, $ ROW, $ FIELD);} Return MySQL_RESULT ($ THIS -> Result, $ row;

Function selectdb ($ dbname = 'test') {/ * Select mysql database * / return mysql_select_db (Empty ($ dbname) $ this -> $ dbname: $ dbname, $ this-> linkid);}

Function stat () {/ * gets the current system status * / return mysql_stat ($ this-> linkid);

Function Tablename ($ index) {/ * gets a table name * / return mysql_tablename ($ this-> Result, $ index);

Function threadid () {/ * Returns the ID * / RETURN MySQL_THREAD_ID ($ this-> linkid);}

Function UnBuffered Query ($ querystring) {/ * Send a SQL query to MySQL, does not get and cache results * / $ this-> Result = mysql_unbuffered_query ($ querystring, $ this-> linkid); return $ this-> Result TRUE: FALSE;} / * - The above method name is one-on-one, except "splitdsn" and "constructor" - * // * - below, That is to say, the following methods are not found in the manual - * / function free () {/ * release result memory, the effect freeereSult, just simple, write a few letters, calculate alias ~ ^ _ ^ * / RETURN $ this-> freresult ();} Function setMustbehalt ($ mustbehalt = false) {$ this-> mustbehalt = $ mustbehalt;

Function getMustbehalt () {RETURN $ this-> mustbehalt;} / * The following is support for things extension * / function setOutocommit ($ autocommit = 1) {/ * default is not supported * / mysql_query ("set autocmit =". $ autocummit }

Function begin () {/ * transaction start * / @Mysql_Query ("Lock Tables"); @Mysql_Query; $ this-> setautocommit (0); mysql_query ("begin");}

Function rollback () {/ * rollback, continue the default non-support * / mysql_query ("rollback"); @Mysql_Query ("unlock Tables"); $ this-> setautocommit (1);}

Function commit () {/ * End, continue the default non-support * / mysql_query ("commit"); @Mysql_Query ("unlock Tables"); $ this-> setautocommit (1);} / * or more is support things Expansion * / function getRows () {/ * gets the number of data records that have been read * / return $ this-> rows;

Function getDbname () {return $ this-> dbname;}

function nextRecord ($ resultType = 0) {/ * phplib with standards, next_record phplib the same * / $ record = NULL; switch ($ resultType) {case 1: $ record = @mysql_fetch_assoc ($ this-> Result); $ this -> Recordcase = 1; Break; Case 2: $ record = @Mysql_Fetch_row ($ this-> result); $ this-> recordcase = 2; break; case 3: $ record = @mysql_fetch_object ($ this-> result); $ this-> Recordcase = 3; Break; default: $ record = @Mysql_Fetch_Array ($ this-> result); $ this-> recordcase = 0; Break;} if (is_resource ($ record)) {$ this-> record = $ Record;} else {returnaf false;}} function f ($ fieldname) {/ * with phplib Limited * / switch ($ this-> recordcase) {casse 3: return is_object ($ this-> record)? $ this -> Record -> $ fieldName: null; break; default: return $ this-> record [$ fieldname]; break;}} / * below you can define format * / function halt ($ msg = "unknown error!" $ Mustbehalt = 0) {Echo $ msg; if ($ mustbehalt! == 0 || $ this-> getMustbehalt ()) {Die ();}} / * can not, because PHP5 is only * / Function __get ($ nm) {if (isset ($ this -> $ nm) ) {//} else {$ this-> Halt ("No member variable: $ nm / n", 1);}}

Function __set ($ nm, $ val) {//}

Function __call ($ m, $ a) {print "


call does not exist -". $ m. "(". Join (",", $ a). ")! / n"; echo '
 '; var_dump ($ a); echo'  '; $ this-> Halt ("
");}

} // End DummyMysqlclass?>

* /// ---------------------------------------------- ----------- // ------------------------------------- --------------------

/ / -------------------------------------------------------------------------------------------- --------- / ** 4 $ DMC-> Query ("Select * from` purchase_productuct` limited 1 "); while ($ fetchobject = $ dmc-> fetchobject (3)) {Echo $ DMC- > fieldName (0). '
'; // Effects such as: ID Echo $ DMC-> FieldType (0). '
'; Echo $ DMC-> Fieldlen (0). '
'; // Effects such as: 9 Print_R ($ DMC-> Fieldflags (0)); // Effects such as NOT_NULL PRIMARY_KEY AUTO_INCREMENT ECHO '


effect:
'; echo '`'. $ Dmc-> FieldName (0). '`'. $ DMC-> FieldType (0). '('. $ dmc-> fieldlen (0). ')' .str_replace (" not_null "," primary_key ", array ( "Not Null", "Primary Key"), Strtoupper ($ DMC-> FieldFlags (0))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))). ", / N
";} // This is a cheat! ! ^ _ ^ $ DMC-> Query ("Show Create Table` Purchase_Product` "); while ($ fetchobject = $ dmc-> fetchobject (3)) {$ t = 'create table'; Print_R ($ FETCHOBJECT -> $ T );} * /// ------------------------------------------- ------------- / ** 4echo "gethostinfo () ---". $ Dmc-> gethostinfo (). '
'; echo "getClientInfo () -". $ DMC-> getClientInfo (). '
'; echo "getProtoInfo () -". $ DMC-> getProtoInfo (). '
'; echo "GetServerInfo () -". $ Dmc-> getServerInfo ). '
'; echo "info () ----------" $ dmc-> info (). '
'; * /// ------- -------------------------------------------------- / ** 3 // Get all tables for the specified library $ DMC-> Listtables ("Test"); while ($ row = $ dmc-> fetchrow ()) {print "Table:". $ Row [0]. " / n ";} // The following for the effect is the same as the While above for ($ I = 0; $ i <$ dmc-> nuMrows (); $ i

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

New Post(0)