Home > Backend Development > PHP Tutorial > clear version of ten examples of dob Adodb

clear version of ten examples of dob Adodb

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:35:53
Original
929 people have browsed it

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.
ADODB has the following advantages (said online, not mine):
1. It is twice as fast as pear;
2. It supports many more database types than pear, and can even support ACCESS;
3. No need Installation, no server support is 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 ADOC connection (ADOConnection) object and used 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 return 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 not used in this example, which can control the number of records displayed (such as Only the first ten records are displayed and 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 a file named northwind's MS-Access database
$recordSet = &$conn->Execute('select * from products'); #Search all data from the products data table
if (!$recordSet)
print $conn->ErrorMsg (); //If an error occurs during data search, 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 in $ The current array is returned in recordSet->fields, and the fields are numerically indexed (starting from 0). We use the MoveNext() function to move to the next record. When the database search reaches the end, the EOF property is set to true. If Execute() An error occurs, recordset returns false.
$recordSet->fields[] array is generated from 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'); // Using 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 CustomerID and OrderDate fields from the Orders table
if (!$recordSet)
print $conn->ErrorMsg(); //If the database search error occurs, the error message is displayed
else
while (!$recordSet->EOF) {
$fld = $recordSet->FetchField(1); //Assign the second field to $fld
$type = $recordSet->MetaType($ fld->type); //Format of getting field value
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 in American standard format Output
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 use the FetchField() function to check the format of the second field. It returns a field containing three The object of the variable
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() converts the database format of the field into a standard field format
C: Character field, which should be displayed under the tag.
X: Text field, which stores relatively large fields Text, generally used in