Home > Article > Backend Development > Ten examples of Adodb (clear version)_PHP tutorial
I originally wanted to learn pear, but I saw several posts on the Internet that rated adodb very highly, so I changed to this one.
The advantages of ADODB include the following (said online, not mine):
1. The speed is twice as fast as pear;
2. It supports many more database types than pear, and can even support ACCESS;
3. No installation or server support required (for novices, this is very important)
Friends who don’t know what adodb is or want to download adodb can go to this link: http:/ /www.phpe.net/class/106.shtml
In addition, if any brother has translated the full text of the README or knows where to find the translation, please reply to me, thank you.
Tutorial
Example 1: Select Statement
Task: Connect to an Access database named Northwind and display the first two fields of each record.
In this example, we created a new An ADOC connection (ADOConnection) object and use it to connect to a database. This connection uses the PConnect method, which is a persistent connection. When we want to query the database, we can call the Execute() function of this connection at any time. It will Returns an ADORecordSet object which is actually a cursor that holds the current row in the array fields[]. We use MoveNext() to move from one record to the next.
NB: There is a very practical function SelectLimit in Not used in this example, it can control the number of records displayed (for example, only the first ten records are displayed, which can be used for paging display).
PHP:---------- -------------------------------------------------- --------------------
include('adodb.inc.php'); #Load ADOdb
$conn = &ADONewConnection('access'); # Create a new connection
$conn->PConnect('northwind'); # Connect to an MS-Access database named northwind
$recordSet = &$conn ->Execute('select * from products'); #Search all data from the products data table
if (!$recordSet)
print $conn->ErrorMsg(); //If data search occurs Error display error message
else
while (!$recordSet->EOF) {
print $recordSet->fields[0].' '.$recordSet->fields[1].'
';
$recordSet->MoveNext(); //Point to the next record
} //List display data
$recordSet->Close(); // Optional
$conn->Close(); //Optional
?>
----------------------- -------------------------------------------------- -------
$recordSet returns the current array in $recordSet->fields, numerically indexing the fields (starting from 0). We use the MoveNext() function to move to the next record. The EOF property is set to true when the database search reaches the end. If an error occurs in Execute(), recordset returns false. The
$recordSet->fields[] array is generated from the PHP database extension. Some database extensions can only be indexed by numbers and not by field names. If you insist on using field name indexes, you should use the SetFetchMode function. Regardless of the format of the index, the recordset can be created by Execute() or SelectLimit().
PHP:--------------------------------------------- ----------------------------------------
$db->SetFetchMode (ADODB_FETCH_NUM);
$rs1 = $db->Execute('select * from table'); //Use numeric index
$db->SetFetchMode(ADODB_FETCH_ASSOC);
$rs2 = $ db->Execute('select * from table'); //Use field name index
print_r($rs1->fields); # shows array([0]=>'v0',[1] =>'v1')
print_r($rs2->fields); # shows array(['col1']=>'v0',['col2'] =>'v1')-- -------------------------------------------------- ----------------------------
If you want to get the record number, you can use $recordSet-> ;RecordCount(). Returns -1 if there is no current record.
Example 2: Advanced Select with Field Objects
Search the table and display the first two fields. If the second field is in time or date format, change it to US standard time format.
PHP:-------------------------------------------------- ------------------------------------
include( 'adodb.inc.php'); ///Load adodb
$conn = &ADONewConnection('access'); //Create a new connection
$conn->PConnect('northwind'); // Connect to the MS-Access database named northwind
$recordSet = &$conn->Execute('select CustomerID,OrderDate from Orders'); //Search the two fields CustomerID and OrderDate from the Orders table
if (!$recordSet)
print $conn->ErrorMsg(); //If the database search error occurs, the error message will be displayed
else
while (!$recordSet->EOF) {
$ fld = $recordSet->FetchField(1); //Assign the second field to $fld
$type = $recordSet->MetaType($fld->type); //Get the field value Format
if ( $type == 'D' || $type == 'T')
print $recordSet->fields[0].' '.
$recordSet-> ;UserDate($recordSet->fields[1],'m/d/Y').'
'; //If the field format is date or time, make it output in the American standard format
else
print $recordSet->fields[0].' '.$recordSet->fields[1].'
'; // Otherwise, output as is
$recordSet- >MoveNext(); //Point to the next record
}
$recordSet->Close(); //Optional
$conn->Close(); //Optional
?>
---------------------------------------- -------------------------------------
In this example , we checked the format of the second field using the FetchField() function. It returned an object containing three variables
name: field name
type: the actual format of the field in its database
max_length: The maximum length of the field. Some databases will not return this value, such as MYSQL. In this case, the max_length value is equal to -1.
We use MetaType() to convert the database format of the field into the standard field format
C: Character field, which should be displayed under the tag.
X: Text field, which stores relatively large text, generally acts on