douzi7711 2009-10-22 07:01
浏览 26
已采纳

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

I have no experience with access.

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

  • 写回答

1条回答 默认 最新

  • dongliyun3301 2009-10-22 07:30
    关注

    PDO

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

    <?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.

    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.

    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.

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序