Data Abstraction Layer-basics of ADODB _ PHP Tutorial

Source: Internet
Author: User
Data Abstraction Layer-basics of ADODB. 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 the simplicity of ActiveDataObjectsDataBase

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]. "";
$ Result-> MoveNext (); // The movenext () array moves down.
} // Fields indicates the field array

Echo "[". $ result-> RecordCount (). "row record returned]";

$ 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]. "";
$ Result-> MoveNext ();
}

// Obtain and display the number of returned Records
Echo "[". $ result-> RecordCount (). "row record returned]";

// 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!

Success! No articles published! This time, I wrote my previous articles to share with you! Don't watch it! ADODB is a summary of Active Data Objects Data Base...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.