Data Abstraction Layer-> Basic Introduction to ADODB, read data abstraction layer-> Basic Introduction to ADODB, which used to be mixed in water! No articles published! This time, I wrote my previous articles to share with you! Don't watch it! ADODB is short for ActiveDataObjectsDataBase. it is an intermediate function component used by PHP to access the database. Use ADODB "> <LINK
I used to mix in the water area! No articles published! This time, I wrote my previous articles to share with you! Don't watch it!
ADODB is short for Active Data Objects Data Base. it is an intermediate function component for accessing the database in PHP.
One of the biggest advantages of using ADODB is that, regardless of the backend database, the methods for accessing the database are the same, so developers do not have, however, you must learn another set of different access methods, which greatly reduces the knowledge burden on developers. the previous knowledge can still be used in the future. when the database platform is transferred, the program code does not have to be too much updated.
These are examples of previous learning summaries!
Copy the PHP content to the clipboard.
PHP code:
Include_once ("libs/adodb. inc. php"); // file containing adodb
$ Db = NewADOConnection ("mysql"); // specifies the database object to be created.
$ Db-> Connect ("localhost", "root", "root", "mysql") or die ("Unable to connect! "); // Link to the database
$ ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; // assign a value to the global variable, indicating that the associated array can be obtained below (my language is not good)
$ Query = "SELECT * FROM user ";
$ Result = $ db-> Execute ($ query) or die ("Error in query: $ query. ". $ db-> ErrorMsg (); // (Execute SQL)
While (! $ Result-> EOF ){
Echo $ result-> fields ['use']. "-". $ result-> fields ['pass']. "\ n ";
$ Result-> MoveNext (); // The movenext () array moves down.
} // Fields indicates the field array
Echo "\ n [". $ result-> RecordCount (). "row record returned] \ n ";
$ Db-> Close (); // Close the link
?>
This example is the simplest database operation of ADODB! Or you can write a class to operate it!
Three functions are provided in adodb: GetAll, GetOne, and GetRow.
GetAll is equivalent to execute, but returns a 2-dimensional array. It can work well with SMARTY.
GetOne and GetRow are used to determine whether a database data exists!
The SelectLimit function is provided in ADODB!
Some SQL statements may not be supported during Database transfer! ADODB uses selectlimit to help us implement it at the underlying layer!
For example:
$ Query = "SELECT * FROM library ";
$ Result = $ db-> SelectLimit ($ query, 5, 3 );
Another point is that ADODB has a caching mechanism.
CacheExecute is provided. Let's take a look at the example!
PHP code:
Include ("../libs/adodb. inc. php ");
// Set the cache storage path, which indicates the current directory
$ ADODB_CACHE_DIR = 'tmp ';
// Create a mysql connection instance object
$ Db = NewADOConnection ("mysql ");
// Open a database connection
$ Db-> Connect ("localhost", "root", "root", "adodb") or die ("cannot Connect ");
// Construct and execute a query
$ Query = "SELECT * FROM user ";
$ Result = $ db-> CacheExecute (300, $ query) or die ("SQL error: $ query. ". $ db-> ErrorMsg (); // CacheExecute (300, $ query) this method is 300, indicating the cache time.
// Traverse the returned record set to display the TITLE and AUTHOR of the column data
While (! $ Result-> EOF ){
Echo $ result-> fields [1]. "-". $ result-> fields [2]. "\ n ";
$ Result-> MoveNext ();
}
// Obtain and display the number of returned Records
Echo "\ n [". $ result-> RecordCount (). "row record returned] \ n ";
// Close the database connection
$ Db-> Close ();
?>
I think there is a conversion method! Rs2html ();
PHP code:
Include_once ('../libs/adodb. inc. php ');
Include_once ("../libs/adodb/tohtml. inc. php ");
// Create a mysql connection instance object
$ Db = NewADOConnection ("mysql ");
// Open a database connection
$ Db-> Connect ("localhost", "root", "root", "user") or die ("database connection error ");
// Construct and execute a query
$ Query = "SELECT * FROM library ";
$ Result = $ db-> Execute ($ query) or die ("SQL error: $ query.". $ db-> ErrorMsg ());
// Returns an HTML table.
Echo rs2html ($ result); // You can check the returned data in the table!
// Close the database connection
$ Db-> Close ();
?>
The following describes several common methods!
RecordCount () gets and displays the number of returned Records
FieldCount () gets and displays the number of returned fields
Insert_ID () displays the best inserted ID
Affected_Rows () displays the number of affected rows after SQL execution.
GetMenu () generate drop-down menu!
Okay, so much to sum up! There is actually a lot of knowledge!
There are still many shortcomings! I hope the old bird will not laugh!
I will try again next time!