首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 操作 Access 資料庫類封裝(ODBC 驅動)完整代碼

     編輯:PHP 綜合知識

類別功能

  • 連線管理(建構子自動連線)
  • 查詢執行 query()、單行取得 first_array()
  • 資料列遍歷 fetch_row()、筆數統計 total_num()
  • CRUD 封裝:insert()、getinfo()、getlist()、getfieldlist()、updateinfo()、deleteinfo()、deleterecord()、getcondrecord()
  • 關閉連線 close()

完整類別代碼

<?php
class Access {
    var $databasepath, $constr, $username, $password, $link;

    function Access($databasepath, $dbusername, $dbpassword) {
        $this->databasepath = $databasepath;
        $this->username = $dbusername;
        $this->password = $dbpassword;
        $this->connect();
    }

    function connect() {
        $this->constr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath($this->databasepath);
        $this->link = odbc_connect($this->constr, $this->username, $this->password, SQL_CUR_USE_ODBC);
        return $this->link;
    }

    function query($sql) {
        return @odbc_exec($this->link, $sql);
    }

    function first_array($sql) {
        return odbc_fetch_array($this->query($sql));
    }

    function fetch_row($query) {
        return odbc_fetch_row($query);
    }

    function total_num($sql) {
        return odbc_num_rows($this->query($sql));
    }

    function close() {
        odbc_close($this->link);
    }

    function insert($table, $field) {
        $temp = explode(',', $field);
        $ins = '';
        for ($i = 0; $i < count($temp); $i++) {
            $ins .= "'" . $_POST[$temp[$i]] . "',";
        }
        $ins = substr($ins, 0, -1);
        $sql = "INSERT INTO $table ($field) VALUES ($ins)";
        $this->query($sql);
    }

    function getinfo($table, $field, $id, $colnum) {
        $sql = "SELECT * FROM $table WHERE $field = $id";
        $query = $this->query($sql);
        if ($this->fetch_row($query)) {
            for ($i = 1; $i < $colnum; $i++) {
                $info[$i] = odbc_result($query, $i);
            }
        }
        return $info;
    }

    function getlist($table, $field, $colnum, $condition, $sort = "ORDER BY id DESC") {
        $sql = "SELECT * FROM $table $condition $sort";
        $query = $this->query($sql);
        $i = 0;
        while ($this->fetch_row($query)) {
            $recordlist[$i] = $this->getinfo($table, $field, odbc_result($query, 1), $colnum);
            $i++;
        }
        return $recordlist;
    }

    function getfieldlist($table, $field, $fieldnum, $condition = "", $sort = "") {
        $sql = "SELECT $field FROM $table $condition $sort";
        $query = $this->query($sql);
        $i = 0;
        while ($this->fetch_row($query)) {
            for ($j = 0; $j < $fieldnum; $j++) {
                $info[$j] = odbc_result($query, $j + 1);
            }
            $rdlist[$i] = $info;
            $i++;
        }
        return $rdlist;
    }

    function updateinfo($table, $field, $id, $set) {
        $sql = "UPDATE $table SET $set WHERE $field = $id";
        $this->query($sql);
    }

    function deleteinfo($table, $field, $id) {
        $sql = "DELETE FROM $table WHERE $field = $id";
        $this->query($sql);
    }

    function deleterecord($table, $condition) {
        $sql = "DELETE FROM $table WHERE $condition";
        $this->query($sql);
    }

    function getcondrecord($table, $condition = "") {
        $sql = "SELECT COUNT(*) AS num FROM $table $condition";
        $query = $this->query($sql);
        $this->fetch_row($query);
        $num = odbc_result($query, 1);
        return $num;
    }
}
?>

使用範例

<?php
$databasepath = "database.mdb";
$dbusername = "";
$dbpassword = "";
include_once("class.php");
$access = new Access($databasepath, $dbusername, $dbpassword);

// 查詢
$rows = $access->getfieldlist("users", "id,name,email", 3, "WHERE active=1");
print_r($rows);

// 新增
$_POST['name'] = "John";
$_POST['email'] = "john@example.com";
$access->insert("users", "name,email");

$access->close();
?>

注意事項

  • 需啟用 PHP odbc 擴展,Windows 預設內建。
  • Access 適用小型專案,高併發效能不如 MySQL/PostgreSQL。
  • insert() 直接使用 $_POST 有安全風險,實際專案請加入驗證與參數化。
  • 建議改用 PDO ODBC 驅動以獲得更統一介面。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved