Dob: common phpADODB usage methods of dob collection: Copy the code as follows: & lt ;? Php defines database variables $ DB_TYPEmysql; $ DB_HOSTlocalhost; $ DB_USERroot; $ DB_PASS; $ DB_DATABASEai-part;
The code is as follows:
// Define database variables
$ DB_TYPE = "mysql ";
$ DB_HOST = "localhost ";
$ DB_USER = "root ";
$ DB_PASS = "";
$ DB_DATABASE = "ai-part ";
Require_once ("../adodb. inc. php ");
$ Db = NewADOConnection ("$ DB_TYPE"); // create a database object
$ Db-> debug = true; // DEBUG test of the database. the default value is false.
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; // return record set form, association form
/***
Returned record set form
Define ('adodb _ FETCH_DEFAULT ', 0 );
Define ('adodb _ FETCH_NUM ', 1 );
Define ('adodb _ FETCH_ASSOC ', 2 );
Define ('adodb _ FETCH_BOTH ', 3 );
The above constant is defined in adodb. inc. php, that is, the "$ ADODB_FETCH_MODE = 2" method is available.
The index in the record set returned by ADODB_FETCH_NUM is in the numerical format, that is, the sort order value of the database fields.
The index in the record set returned by ADODB_FETCH_ASSOC is the field name of the original database.
ADODB_FETCH_BOTH and ADODB_FETCH_DEFAULT both return the preceding two types. Some databases do not support
An example:
$ ADODB_FETCH_MODE = ADODB_FETCH_NUM;
$ Rs1 = $ db-> Execute ('select * from table ');
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
$ Rs2 = $ db-> Execute ('select * from table ');
Print_r ($ rs1-> fields); # The returned array is array ([0] => 'v0', [1] => 'v1 ')
Print_r ($ rs2-> fields); # The returned array is: array (['col1'] => 'v0', ['col2'] => 'v1 ')
***/
// Connect to the database. The methods include Connect, PConnect, and NConnect. Connect is generally used.
If (! @ $ Db-> Connect ("$ DB_HOST", "$ DB_USER", "$ DB_PASS", "$ DB_DATABASE ")){
Exit ('server busy, please visit later ');
}
/*
$ Db-> $ rs-> usage of this class
Execute ($ SQL), Execute the $ SQL statement in the parameter
SelectLimit ($ SQL, $ numrows =-1, $ offset =-1) $ numrows: returns several records, $ offset, starting from the number of records, which are generally used for paging, or use
*/
// Example: retrieve multiple records
$ SQL = "Select * FROM table orDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL) {// Execute the SQL statement and return the result to the $ rs variable.
Echo $ db-> ErrorMsg (); // The error message is printed.
$ Db-> Close (); // Close the database
Exit ();
}
While (! $ Rs-> EOF) {// traverses the record set
Echo $ rs-> fields ['username'].'
';
// Try print_r ($ rs-> fields). $ rs-> fields ['Field name'] returns the value in this field.
$ Rs-> MoveNext (); // point the pointer to the next record. Otherwise, an endless loop occurs!
}
$ Rs-> Close (); // Close to release memory
// Insert a new record
$ SQL = "Insert table (user_type, username) VALUES (3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
// Update records
$ SQL = "Update table SET user_type = 3 Where id = 2 ";
$ Db-> Execute ($ SQL );
// Delete a record
$ SQL = "Delete FROM table Where id = 2 ";
$ Db-> Execute ($ SQL );
// Retrieve a single record
// $ Db-> GetRow ($ SQL), obtain the first record, and return an array. If an error occurs, false is returned.
$ SQL = "Select username, password, user_type FROM table Where id = 3 ";
$ Data_ary = $ db-> GetRow ($ SQL );
If ($ data_ary = false ){
Echo 'This record is not found ';
Exit ();
} Else {
Echo $ data_ary ['username']. ''. $ data_ary ['password'].''. $ data_ary ['User _ type'].'
';
}
// Another method
$ SQL = "Select username, password, user_type FROM table Where id = 3 ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
If (! $ Result = $ rs-> FetchRow ()){
Echo 'This record is not found ';
Exit ();
} Else {
Echo $ result ['username']. ''. $ result ['password'].''. $ result ['User _ type'].'
';
}
// Retrieve a single field
// $ Db-> GetOne ($ SQL) retrieves the value of the first field of the first record. If an error occurs, false is returned.
$ SQL = "Select COUNT (id) FROM table ";
$ Record_nums = $ db-> GetOne ($ SQL );
Echo $ record_nums;
$ SQL = "Select username, password, user_type FROM table Where user_id = 1 ";
$ Result = $ db-> GetOne ($ SQL );
Echo $ result; // Print the username value
/*
When adding, modifying, and deleting records,
To process string fields, use $ db-> qstr () to process user input characters,
Data judgment is required for numeric fields
Update record. note: This is the case where magic_quotes is set to Off in php. ini. if you are not sure, you can use
$ Db-> qstr ($ content, get_magic_quotes_gpc ())
Note: c
*/
$ SQL = "Update table SET c
$ Db-> Execute ($ SQL );
/* $ Db-> Insert_ID (), no parameter. the ID value of the record just inserted is returned. only some databases and databases with the auto-increment function are supported, such as PostgreSQL, mySQL and MS SQL
*/
// Example:
$ SQL = "Insert table (user_type, username) VALUES (3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
$ Data_id = $ db-> Insert_ID ();
Echo $ data_id;
/* $ Db-> GenID ($ seqName = 'adodbseq ', $ startID = 1) to generate an ID value. $ seqName: name of the database table used to generate this ID. $ startID: start value. Generally, you do not need to set it. it automatically adds the value in $ seqName to 1. some databases are supported. some databases are not supported.
Insert_ID, GenID. Generally, I use GenID, which is used only when the ID of a record is to be obtained immediately after it is inserted.
*/
/* Example:
Create a table named user_id_seq with only one field, id, int (10), not null, and insert a record with a value of 0.
*/
$ User_id = $ db-> GenID ('User _ id_seq ');
$ SQL = "Insert table (id, user_type, username) VALUES (". $ user_id. ", 3, 'lieng eng ')";
$ Db-> Execute ($ SQL );
/*
$ Rs-> RecordCount (), retrieves the total number of record sets, no parameter
It seems that the number of retrieved records is obtained using the count () array method.
If a large amount of data is retrieved, the efficiency is slow. we recommend that you use the COUNT (*) method in SQL.
$ SQL = "Select COUNT (*) FROM table". when using this method, do not add order by to SQL, which will reduce the execution speed.
Example:
*/
$ SQL = "Select * FROM table orDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
$ Record_nums = $ rs-> RecordCount ();
/*
If you want to perform the same loop processing for a result set twice, you can use the following method:
The following is an example. it only describes how to use $ rs-> MoveFirst ().
*/
$ SQL = "Select * FROM table orDER BY id DESC ";
If (! $ Rs = $ db-> Execute ($ SQL )){
Echo $ db-> ErrorMsg ();
$ Db-> Close ();
Exit ();
}
$ Username_ary = array ();
While (! $ Rs-> EOF ){
$ Username_ary [] = $ rs-> fields ['username']
Echo $ rs-> fields ['username'].'
'; // Print_r ($ rs-> fields). try $ rs-> fields ['Field name'] and return the value in this field.
$ Rs-> MoveNext (); // point the pointer to the next record. if you don't need it, an endless loop will occur!
}
$ Username_ary = array_unique ($ username_ary );
$ Rs-> MoveFirst (); // returns the pointer to the first record.
While (! $ Rs-> EOF ){
Echo $ rs-> fields ['password'].'
'; // Print_r ($ rs-> fields). try $ rs-> fields ['Field name'] and return the value in this field.
$ Rs-> MoveNext (); // pointer to the next record
}
$ Rs-> Close ();
// When the operation on the database of the program on this page is completed, $ db-> Close () is required ();
$ Db-> Close ();
/* A good method */
If (isset ($ db )){
$ Db-> Close ();
}
?>
The above section introduces the commonly used php ADODB usage methods of dob, including dob content, and hopes to help those who are interested in PHP tutorials.