|
【示例】PHP通過ODBC連接數據庫 |
118.117.20.*
|
<?php $dsn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=E:\\Databases\data.mdb"; $conn = odbc_connect($dsn, "", "") or die ("Failed connecting to the Access Database"); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Microsoft Access 2003</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css"> <!-- body,td,th { font-size: 13px; font-family: Arial, Helvetica, sans-serif; } --> </style></head> <body> <?php $sql = "SELECT * FROM Products ORDER BY ProductID ASC"; $rs = odbc_exec($conn, $sql); ?> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#66CC00"> <tr> <td width="60" height="21" align="center" bgcolor="#E7FFD7">產品編號</td> <td height="21" align="center" bgcolor="#E7FFD7">產品名稱</td> <td width="200" align="center" bgcolor="#E7FFD7">產品描述</td> <td width="70" height="21" align="center" bgcolor="#E7FFD7">產品價格</td> </tr> <?php while ($row = odbc_fetch_array($rs)) { ?> <tr> <td width="60" height="28" align="center" bgcolor="#E7FFD7"><?=$row["ProductID"]?></td> <td height="28" align="center" bgcolor="#FFFFFF"><?=$row["ProductName"]?></td> <td width="200" align="center" bgcolor="#FFFFFF"><?=$row["ProductDescription"]?></td> <td width="70" height="28" align="center" bgcolor="#FFFFFF">$<?=$row["ProductPrice"]?></td> </tr> <?php } ?> </table> </body> </html> <?php odbc_close($conn); ?>
|
118.117.20.*
|
輸出內容:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Microsoft Access 2003</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css"> <!-- body,td,th { font-size: 13px; font-family: Arial, Helvetica, sans-serif; } --> </style></head> <body> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#66CC00"> <tr> <td width="60" height="21" align="center" bgcolor="#E7FFD7">產品編號</td> <td height="21" align="center" bgcolor="#E7FFD7">產品名稱</td> <td width="200" align="center" bgcolor="#E7FFD7">產品描述</td> <td width="70" height="21" align="center" bgcolor="#E7FFD7">產品價格</td> </tr> <tr> <td width="60" height="28" align="center" bgcolor="#E7FFD7">1</td> <td height="28" align="center" bgcolor="#FFFFFF">Android</td> <td width="200" align="center" bgcolor="#FFFFFF">An android book</td> <td width="70" height="28" align="center" bgcolor="#FFFFFF">$5.85</td> </tr> <tr> <td width="60" height="28" align="center" bgcolor="#E7FFD7">2</td> <td height="28" align="center" bgcolor="#FFFFFF">Spanish</td> <td width="200" align="center" bgcolor="#FFFFFF">For Spanish learners</td> <td width="70" height="28" align="center" bgcolor="#FFFFFF">$10.73</td> </tr> <tr> <td width="60" height="28" align="center" bgcolor="#E7FFD7">3</td> <td height="28" align="center" bgcolor="#FFFFFF">English</td> <td width="200" align="center" bgcolor="#FFFFFF">For English beginners</td> <td width="70" height="28" align="center" bgcolor="#FFFFFF">$40.5</td> </tr> <tr> <td width="60" height="28" align="center" bgcolor="#E7FFD7">4</td> <td height="28" align="center" bgcolor="#FFFFFF">PHP</td> <td width="200" align="center" bgcolor="#FFFFFF">A PHP Tutorial</td> <td width="70" height="28" align="center" bgcolor="#FFFFFF">$15.36</td> </tr> </table> </body> </html>
|
118.117.20.*
|
這是一個PHP連接Access 2003數據庫的示例。
|
118.117.20.*
|
$conn = odbc_connect($dsn, "", "", SQL_CUR_USE_ODBC) or die ("Failed connecting to the Access Database");
用SQL_CUR_USE_ODBC可以避免一些錯誤。
|
118.117.20.*
|
...
<p><?=odbc_num_rows($rs)?></p> </body> </html> <?php odbc_free_result($rs); odbc_close($conn); ?>
|
118.117.20.*
|
mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放。
|