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>";
}