I used to always hang out in the water area! No articles published! This time I will share with you the articles I have written before! Haha, old bird, stop reading!
ADODB is the abbreviation of Active Data Objects Data Base, which is an intermediate function component for PHP to access the database.
One of the biggest advantages of using ADODB is that regardless of the back-end database, the way to access the database is the same. Developers and designers do not have to learn another set of different access methods for a certain set of databases, which greatly Reducing the knowledge burden on developers, past knowledge can still be used in the future, and the program code does not need to be significantly changed when the database platform is transferred.
These are some examples of summaries from previous studies!
Copy PHP content to clipboard
PHP code:
<?php
include_once("libs/adodb/adodb.inc.php"); //The file containing adodb
$db = NewADOConnection("mysql"); //Which database object to create
$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 you can create an associative array below (my language is not very good) )
$query = "SELECT * FROM user";
$result = $db->Execute($query) or die("Error in query: $query. " . $db->ErrorMsg());//(Execute executes SQL)
while (!$result->EOF) {
echo $result->fields['use'] . " - " . $result->fields['pass'] . "n";
$result->MoveNext();//movenext() array moves down.
}//fields represents the field array
echo "n[" . $result->RecordCount() . " row records are 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 actually equivalent to execute, but the return is a 2-dimensional array. Can work well with SMARTY.
GetOne, GetRow. is to determine whether a certain data in the database exists!
The SelectLimit function is provided in ADODB!
Because some SQL statements may not be supported when transferring the database! ADODB uses selectlimit to help us implement it at the bottom level!
For example:
$query = "SELECT * FROM library";
$result = $db->SelectLimit($query, 5, 3);
Another point is that ADODB has a caching mechanism.
CacheExecute is provided. Haha, let’s take a look at the example!
PHP code:
<?php
include("../libs/adodb/adodb.inc.php");
//Set the cache saving path, . 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, which means How long the cache exists.
// Traverse the returned recordset and display the contents of column data TITLE and AUTHOR
while (!$result->EOF) {
echo $result->fields[1] . " - " . $result->fields[2] . "n";
$result->MoveNext();
}
// Get and display the number of returned record rows
echo "n[" . $result->RecordCount() . " Row records are returned]n";
// Close the database connection
$db->Close();
?>
What I feel most comfortable about is that there is a conversion method! rs2html();
PHP code:
<html>
<head></head>
<body>
<?php
include_once('../libs/adodb/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());
// Return a table in HTML format
echo rs2html($result);//See the returned data in the table for yourself!
//Close database connection
$db->Close();
?>
</body>
</html>
Here are some commonly used methods!
RecordCount() obtains and displays the number of returned record rows
FieldCount() obtains and displays the number of fields returned
Insert_ID() shows the best inserted ID
Affected_Rows() displays the number of affected rows after executing SQL
GetMenu() generates a drop-down menu!
Okay, that’s all I can summarize! In fact, there is a lot of knowledge!
There are still many shortcomings! I hope the old bird won’t laugh!
I’ll add more to everyone next time!