当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > PHP ODBC
ODBC is an Application Programming Interface (API) that allows you to connect to a data source (e.g. an MS Access database).
ODBC (OpenDatabaseConnectivity,开放数据库互连),它是一个应用程序编程界面[Application Programming Interface (API)],它可以直接连接到一个数据源[data source](如:MS Access数据库)。
With an ODBC connection, you can connect to any database, on any computer in your network, as long as an ODBC connection is available.
建立了ODBC连接之后,只要ODBC的连接有效,你就可以连接到网络上任意一台计算机上的数据库中。
Here is how to create an ODBC connection to a MS Access Database:
下面列举一下创建一个ODBC并将其连接到MS Access数据库的方法:
Note that this configuration has to be done on the computer where your web site is located. If you are running Internet Information Server (IIS) on your own computer, the instructions above will work, but if your web site is located on a remote server, you have to have physical access to that server, or ask your web host to to set up a DSN for you to use.
注意:这些设置必须在你网站所在的计算机上进行操作。如果你的计算机上启动了“网络信息服务器[IIS]”,上述指令将会正常运行;但是,如果你的网站位于远程服务器上,你必须先连接远程服务器,然后设置一个你需要使用的DSN。
The odbc_connect() function is used to connect to an ODBC data source. The function takes four parameters: the data source name, username, password, and an optional cursor type.
odbc_connect()函数是用来与ODBC数据源建立连接的。这个函数包含四个参数:数据源名称、用户名、密码、一个“选择指针”的类型。
The odbc_exec() function is used to execute an SQL statement.
odbc_exec()函数是用来执行一条SQL语句的。
The following example creates a connection to a DSN called northwind, with no username and no password. It then creates an SQL and executes it:
下面的案例创建了一个名为“northwind”的DNS连接,它不包含用户名和密码;它创建了一个SQL并执行它,具体如下:
$conn=odbc_connect('northwind','',''); $sql="SELECT * FROM customers"; $rs=odbc_exec($conn,$sql); |
The odbc_fetch_rows() function is used to return records from the result-set. This function returns true if it is able to return rows, otherwise false.
odbc_fetch_rows()函数用来从“result-set[rs]”中获取记录的;如果可以返回记录行,这个函数则返回true,否则反回flase。
The function takes two parameters: the ODBC result identifier and an optional row number:
这个函数包含两个参数:ODBC结果检验器[ODBC result identifier]和一个指定记录行的数量[an optional row number]:
odbc_fetch_row($rs) |
The odbc_result() function is used to read fields from a record. This function takes two parameters: the ODBC result identifier and a field number or name.
odbc_result()函数是用来从一条记录中获取字段的。这个函数包含两个参数:ODBC结果检验器[ODBC result identifier]和字段数量及名称[a field number or name]
The code line below returns the value of the first field from the record:
下面的代码从记录中返回了第一个字段的名称:
$compname=odbc_result($rs,1); |
The code line below returns the value of a field called "CompanyName":
下面的代码返回了名为“CompanyName”的字段值:
$compname=odbc_result($rs,"CompanyName"); |
The odbc_close() function is used to close an ODBC connection.
odbc_close()函数是用于关闭一个ODBC连接的。
odbc_close($conn); |
The following example shows how to first create a database connection, then a result-set, and then display the data in an HTML table.
下面的案例展示了如何创建一个数据库连接和result-set,并将数据显示在HTML表格内:
<html> <body> <?php $conn=odbc_connect('northwind','',''); if (!$conn) {exit("Connection Failed: " . $conn);} $sql="SELECT * FROM customers"; $rs=odbc_exec($conn,$sql); if (!$rs) {exit("Error in SQL");} echo "<table><tr>"; echo "<th>Companyname</th>"; echo "<th>Contactname</th></tr>"; while (odbc_fetch_row($rs)) { $compname=odbc_result($rs,"CompanyName"); $conname=odbc_result($rs,"ContactName"); echo "<tr><td>$compname</td>"; echo "<td>$conname</td></tr>"; } odbc_close($conn); echo "</table>"; ?> </body> </html> |