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数据库)。


Create an ODBC Connection
建立一个ODBC连接

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数据库的方法:

  1. Open the Administrative Tools icon in your Control Panel.
    在“控制面板”上打开“管理工具”
  2. Double-click on the Data Sources (ODBC) icon inside.
     双击里面的“数据源(ODBC)”图标
  3. Choose the System DSN tab.
    选择“系统DSN”标签
  4. Click on Add in the System DSN tab.
    在这个标签内显示的菜单内点击“添加”按钮
  5. Select the Microsoft Access Driver. Click Finish.
    选择“Microsoft Access Driver”后点击“完成”
  6. In the next screen, click Select to locate the database.
    在下一个跳出的窗口中,点击“Select”来定位你的数据库
  7. Give the database a Data Source Name (DSN).
    为你的数据库的数据源命名
  8. Click OK.
    点击“确定”

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。


Connecting to an ODBC
与ODBC连接

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语句的。

Example
案例

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);


Retrieving Records
获取记录

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)


Retrieving Fields from a Record
从记录中获取字段

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


Closing an ODBC Connection
关闭ODBC连接

The odbc_close() function is used to close an ODBC connection.
odbc_close()函数是用于关闭一个ODBC连接的。

odbc_close($conn);


An ODBC Example
ODBC案例

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值