【问题标题】:What will be connection string to Access database file with PHP什么将是使用 PHP 访问数据库文件的连接字符串
【发布时间】:2011-09-13 18:53:53
【问题描述】:

我安装了 WAMP,我在项目文件夹中有 access 数据库文件,但我的计算机上没有安装 Access。

即使我没有安装 Access,我也可以使用 PHP 读取和更新 Access 文件吗?

Access 数据库文件的连接字符串是什么?

我真的需要这方面的帮助。

【问题讨论】:

    标签: php ms-access connection-string


    【解决方案1】:
    <?php
    
    $db = $_SERVER["DOCUMENT_ROOT"] ."/AccessDatabase/reg.accdb"; //AccessDatabase is folder in htdocs where the database is store 
    if (!file_exists($db))
    {
           die("No database file.");
    }
    
    $dbNew = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$db; Uid=; Pwd=;");
    $sql = "select * from reg"; //reg is table name
    $rs = $dbNew->query($sql);
    
    while($result = $rs->fetch())
    {
         echo $result[0].": ".$result[1].": ".$result[2]."<br />";
    } 
    
    
    ?>
    

    如果您遇到诸如 pdo ODBC Drivers not installed 之类的错误 只需转到 php.ini 并找到 extension = pdo_ODBC Driver 并删除注释(;) 之后重启 apache

    【讨论】:

    • 请用正确的英语多解释一下,以便您的回答对读者更有用,谢谢
    【解决方案2】:

    //微软访问

    1. 在控制面板中打开管理工具图标。
    2. 双击里面的数据源 (ODBC) 图标。
    3. 选择系统 DSN 选项卡。
    4. 单击“系统 DSN”选项卡中的“添加”。
    5. 选择 Microsoft Access 驱动程序。
    6. 点击完成。
    7. 在下一个屏幕中,单击“选择”以定位数据库。
    8. 为数据库指定一个数据源名称 (DSN)。
    9. 点击确定。

      $dsn='database.accdb';
      $username='';
      $password='';
      $connect=odbc_connect($dsn, $username, $password);
      

    【讨论】:

      【解决方案3】:

      我找到了this 链接,其中包含有关如何操作的教程。请注意,Windows 和 UNIX 环境中的工作方式有所不同,但是由于您使用的是 WAMP,所以应该没有问题

      【讨论】:

        【解决方案4】:

        您只需要PHP api for ODBC。 以下是文档本身的示例:

        <?php
        // Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
        $connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=$server;Database=$database;", $user, $password);
        
        // Microsoft Access
        $connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);
        
        // Microsoft Excel
        $excelFile = realpath('C:/ExcelData.xls');
        $excelDir = dirname($excelFile);
        $connection = odbc_connect("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=$excelFile;DefaultDir=$excelDir" , '', '');
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-02
          • 1970-01-01
          • 2011-08-22
          • 2023-03-03
          • 1970-01-01
          相关资源
          最近更新 更多