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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP