PHP和Microsoft Access数据库 - 连接和CRUD

时间:2022-08-31 04:34:49

I have no experience with access.

我没有访问权限。

How to do update/insert/delete/select statement with and without $rs = new com("ADODB.RecordSet"); ?

如何使用和不使用$ rs = new com(“ADODB.RecordSet”)进行更新/插入/删除/选择语句; ?

1 个解决方案

#1


13  

PDO

If you want to interface with an MS Access database using PHP, PDO is available for you.

如果要使用PHP与MS Access数据库连接,可以使用PDO。

<?php
    try {
        $pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin");
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    } 

When using PDO, due to the unified interface for DB operations, you have the opportunity to make your app more portable across various RDBMs systems. All you have to do is to provide the connection string to the PDO new instance and have the correct PDO driver installed.

使用PDO时,由于数据库操作的统一界面,您有机会在各种RDBM系统中使您的应用程序更具可移植性。您所要做的就是为PDO新实例提供连接字符串并安装正确的PDO驱动程序。

As the result of this unified interface, your application can be easily ported from MS Access to MySQL, SQLite, Oracle, Informix, DB2, etc. which most certainly is the case if it ages enough.

作为这个统一界面的结果,您的应用程序可以轻松地从MS Access移植到MySQL,SQLite,Oracle,Informix,DB2等等,如果它足够老化,情况肯定是这样的。

Here's an insertion example:

这是一个插入示例:

<?php
try {
   // Connect, 
   // Assuming that the DB file is available in `C:\animals.mdb`
   $pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");

    // INSERT data
    $count = $pdo->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");

    // echo the number of affected rows
    echo $count;

    // close the database connection
    // See: http://php.net/manual/en/pdo.connections.php
    $pdo = null;
}
catch (PDOException $e) {
    echo $e->getMessage();
}

ODBC

In case you don't want to use PDO for some insane reasons, you can look into ODBC.

如果您出于某种疯狂的原因不想使用PDO,可以查看ODBC。

Here's an example:

这是一个例子:

<?php

if (! $conn = odbc_connect('northwind', '', '')) {
    exit("Connection Failed: $conn");
}

if (! $rs = odbc_exec($conn, 'SELECT * FROM customers')) {
    exit('Error in SQL');
}

while (odbc_fetch_row($rs)) {
  echo 'Company name: ', odbc_result($rs, 'CompanyName'), PHP_EOL;
  echo 'Contact name: ', odbc_result($rs, 'ContactName'), PHP_EOL;
}

odbc_close($conn);

#1


13  

PDO

If you want to interface with an MS Access database using PHP, PDO is available for you.

如果要使用PHP与MS Access数据库连接,可以使用PDO。

<?php
    try {
        $pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin");
    }
    catch (PDOException $e) {
        echo $e->getMessage();
    } 

When using PDO, due to the unified interface for DB operations, you have the opportunity to make your app more portable across various RDBMs systems. All you have to do is to provide the connection string to the PDO new instance and have the correct PDO driver installed.

使用PDO时,由于数据库操作的统一界面,您有机会在各种RDBM系统中使您的应用程序更具可移植性。您所要做的就是为PDO新实例提供连接字符串并安装正确的PDO驱动程序。

As the result of this unified interface, your application can be easily ported from MS Access to MySQL, SQLite, Oracle, Informix, DB2, etc. which most certainly is the case if it ages enough.

作为这个统一界面的结果,您的应用程序可以轻松地从MS Access移植到MySQL,SQLite,Oracle,Informix,DB2等等,如果它足够老化,情况肯定是这样的。

Here's an insertion example:

这是一个插入示例:

<?php
try {
   // Connect, 
   // Assuming that the DB file is available in `C:\animals.mdb`
   $pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");

    // INSERT data
    $count = $pdo->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");

    // echo the number of affected rows
    echo $count;

    // close the database connection
    // See: http://php.net/manual/en/pdo.connections.php
    $pdo = null;
}
catch (PDOException $e) {
    echo $e->getMessage();
}

ODBC

In case you don't want to use PDO for some insane reasons, you can look into ODBC.

如果您出于某种疯狂的原因不想使用PDO,可以查看ODBC。

Here's an example:

这是一个例子:

<?php

if (! $conn = odbc_connect('northwind', '', '')) {
    exit("Connection Failed: $conn");
}

if (! $rs = odbc_exec($conn, 'SELECT * FROM customers')) {
    exit('Error in SQL');
}

while (odbc_fetch_row($rs)) {
  echo 'Company name: ', odbc_result($rs, 'CompanyName'), PHP_EOL;
  echo 'Contact name: ', odbc_result($rs, 'ContactName'), PHP_EOL;
}

odbc_close($conn);