include("$adodb_path/adodb.inc.php"); // includes the adodb library
$db = NewADOConnection('$database_type'); // A new connection
$db->Connect("$host", "$user", "$password", "$database_name");
如今你已具有一个数据库毗连对象 $db 了。 你也能够利用 ADONewConnection 来交换 NewADOConnection ―― 这两个是统一函数的分歧的名字。 毗连的数据库变量 $database_type 需求针对你的实践情形改成你所需求的。可使用以以下表中的一个(括号内的为描写局部,不要在代码中利用):
access (Microsoft Access/Jet)
ado (Generic ADO, the base for all the other ADO drivers)
ado_access (Microsoft Access/Jet using ADO)
ado_mssql (Microsoft SQL Server using ADO)
db2 (DB2)
vfp (Microsoft Visual FoxPro)
fbsql (FrontBase)
ibase (Interbase 6 or before)
firebird (Firebird)
informix72 (Informix databases before Informix 7.3)
informix (Informix)
maxsql (MySQL with transaction support)
mssql (Microsoft SQL Server 7)
mssqlpo (Portable mssql driver)
mysql (MySQL without transaction support)
mysqlt (MySQL with transaction support, identical to maxmysql)
oci8 (Oracle 8/9)
oci805 (Oracle 8.0.5)
oci8po (Oracle 8/9 portable driver)
odbc (Generic ODBC, the base for all the other ODBC drivers)
odbc_mssql (MSSQL via ODBC)
odbc_oracle (Oracle via ODBC)
oracle (Oracle 7)
postgres (PostgreSQL)
postgres64 (PostgreSQL 6.4)
postgres7 (PostgreSQL 7, currently identical to postgres )
sqlanywhere (Sybase SQL Anywhere)
sybase (Sybase)
假如你的链接代码呈现了毛病的提醒,那末你起首要反省的中央就是在途径或毗连的变量上。在你指摘 ADOdb 之前,请肯定你是已准确的利用那些变量。(良多伴侣常花太多工夫去修改这些不言而喻的毛病。) 假如毗连没有任何毛病提醒,那末咱们如今已可以在咱们的项目中来利用 ADodb 了。
$sql = "SELECT surname, age FROM employees";
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields[0].' '.$rs->fields[1].'<BR>';
// fields[0] is surname, fields[1] is age
$rs->MoveNext(); // Moves to the next row
}
}
$sql = "SELECT surname, age FROM employees";
$db->SetFetchMode(ADODB_FETCH_ASSOC); // Return associative array
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
else {
while (!$rs->EOF) {
print $rs->fields['surname']." ".$rs->fields['age']."<BR>";
$rs->MoveNext(); // Moves to the next row
} // end while
} // end else
$sql = "SELECT surname, age FROM employees";
$db->SetFetchMode(ADODB_FETCH_ASSOC); // Return associative array
$rs = &$db->Execute($sql);
if (!$rs) {
print $db->ErrorMsg(); // Displays the error message if no results could be returned
}
// loop through results
while ($row = $rs->FetchNextObject()) {
// The field names need to be uppercase
print $row->SURNAME." ".$row->AGE."<BR>";
}
拔出、更新纪录
根基的 INSERT 操作便利、快捷, 具有与SELECT一样的语法。
$sql = "INSERT INTO employees (surname, age) values ('Clegg','43')";
if (!($db->Execute($sql))) {
print 'Error inserting: '.$db->ErrorMsg().'<BR>';
}
$employee_surname = $db->qstr("d'Angelo");
$arrival_time = $db->DBDate(time());
// The above two functions also add the enclosing quotes, so, $arrival_time, not '$arrival_time'
$sql = "INSERT INTO employee_arrival (arrival_time,surname) values ($arrival_time,$employee_surname)";
if (!($db->Execute($sql))) {
print 'Error inserting: '.$db->ErrorMsg().'<BR>';
}
你可以以完整不异的体例更新数据库,举例来讲:
$sql = "UPDATE employees SET age='44' WHERE id='121')";
if (!($db->Execute($sql))) {
print 'Error updating: '.$db->ErrorMsg().'<BR>';
}