Home  >  Article  >  Backend Development  >  PHP and database ODBC_PHP tutorial

PHP and database ODBC_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:03:57996browse

ODBC for PHP database

ODBC is an application programming interface (API) that allows you to connect to a data source (such as an MS Access database).


-------------------------------------------------- -------------------------------

Create an ODBC connection
Thanks to ODBC connectivity, you can connect to any database, on any computer, in your network, as long as an ODBC connection is available.

Here's how to create an ODBC connection to an MS Access repository:

Open the Administrative Tools icon in your Control Panel.
Double-click inside the data source (ODBC) icon.
Select the system DSN label.
Click Add System DSN Label.
Select the Microsoft Access driver. Click Finish.
On the next screen, click Select Find Database.
Give the database the data source name (DSN).
Click OK.
Please note that this configuration needs to be done on the computer where your website is located. If you are running Internet Information Server (IIS) on your own computer, the above instructions will work, but if your website is located on a remote server, you will have to have actual contact with that server, or ask your web host, to create a DSN for your use.


-------------------------------------------------- -------------------------------

Connect to an ODBC
The odbc_connect() function is used to connect to ODBC data sources. This function has four parameters: data source name, username, password and an optional cursor type.

The odbc_exec() function is used to execute SQL statements.

For example
The following example creates a connection to a DSN called Beifeng, with no username and no password. Then it creates a database and executes it:

$conn=odbc_connect('northwind','','');
$sql="SELECT * FROM customers"; 
$rs=odbc_exec($conn,$sql);
<p>检索记录<br>该odbc_fetch_row ( )函数是用来记录返回的结果集。此函数返回true如果它能够返回行,否则假的。 <br><br>该功能需要两个参数:结果的ODBC识别和可选的连续号码:</p><p> </p><pre class="brush:php;toolbar:false">odbc_fetch_row($rs)
 
<p>检索字段的纪录<br>该odbc_result ( )函数是用来读取领域的纪录。此功能需要两个参数:结果的ODBC标识和一个外地号码或名字。 <br><br>代码线以下的回报价值的第一次实地的记录:</p><p> </p><pre class="brush:php;toolbar:false">$compname=odbc_result($rs,1);
<p>代码线以下的回报价值的领域所谓的“公司名称” :</p><p> </p><pre class="brush:php;toolbar:false">$compname=odbc_result($rs,"CompanyName");
 
<p>闭幕的ODBC连接<br>该odbc_close ( )函数是用来关闭ODBC连接。</p><p> </p><pre class="brush:php;toolbar:false">odbc_close($conn);
 
<p>例如一个ODBC <br>下面的例子显示了如何首先创建一个数据库连接,然后结果集,然后显示数据的HTML表格。</p><p> </p><pre class="brush:php;toolbar:false"><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>
 
转载请注明来自: www.111cn.cn/phper/php.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630900.htmlTechArticlePHP Database ODBC ODBC is an application programming interface (API) that allows you to connect to a data source (such as MS Access database). ------------------------------------------...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn