目前共有6篇帖子。 內容轉換:不轉換▼
 
點擊 回復
230 5
【示例】PHP通過ODBC連接數據庫
118.117.20.*
1樓 發表于:2015-6-14 14:18
<?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.*
2樓 發表于:2015-6-14 14:19

輸出內容:

<!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.*
3樓 發表于:2015-6-14 14:21
這是一個PHP連接Access 2003數據庫的示例。
118.117.20.*
4樓 發表于:2015-6-14 16:14

$conn = odbc_connect($dsn, "", "", SQL_CUR_USE_ODBC)
    or die ("Failed connecting to the Access Database");

用SQL_CUR_USE_ODBC可以避免一些錯誤。

118.117.20.*
5樓 發表于:2015-6-14 16:18

...

<p><?=odbc_num_rows($rs)?></p>
</body>
</html>
<?php
odbc_free_result($rs);
odbc_close($conn);
?>

118.117.20.*
6樓 發表于:2015-6-14 16:19
mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放。

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:230 回複數:5
評論數: ?
作者: 118.117.20.*
最後回復:118.117.20.*
最後回復時間:2015-6-14 16:19
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。