如何将 PHP 与 Microsoft Access 数据库连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19807081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:04:30  来源:igfitidea点击:

How to connect PHP with Microsoft Access database

phpms-accessodbc

提问by

I am currently faced with a new challenge to develop a site using Microsoft Access as the primary database instead of mysql. I have not used MS Access before and I would like guidiance on how to go about it, I have looked up the w3c website on W3schoolsbut the code gives error

我目前面临着使用 Microsoft Access 作为主数据库而不是 mysql 来开发站点的新挑战。我以前没有使用过 MS Access,我想获得有关如何使用它的指导,我在W3schools上查找了 w3c 网站,但代码给出了错误

Warning: odbc_connect() [function.odbc-connect]: SQL error: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in C:\Users\NNALI\Desktop\root\test.php on line 2

警告:odbc_connect() [function.odbc-connect]:SQL 错误:[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序,C:\Users\NNALI\Desktop\ 中的 SQLConnect 中的 SQL 状态为 IM002第 2 行的 root\test.php

and this error

和这个错误

Warning: odbc_exec() expects parameter 1 to be resource, boolean given in C:\Users\NNALI\Desktop\Breweries\root\test.php on line 4

警告:odbc_exec() 期望参数 1 是资源,布尔值在 C:\Users\NNALI\Desktop\Breweries\root\test.php 第 4 行中给出

I am stuck and do not know what to do, I would appreciate all help on this.

我被困住了,不知道该怎么办,我将不胜感激。

<?php
    $conc = odbc_connect("northwind", "","");
    $sql  = "Select * From customers";
    $rs   = odbc_exec($conn, $sql);
?>

Above is the code I used

以上是我使用的代码

回答by Gord Thompson

If you are just getting started with a new project then I would suggest that you use PDO instead of the old odbc_exec()approach. Here is a simple example:

如果您刚刚开始一个新项目,那么我建议您使用 PDO 而不是旧odbc_exec()方法。这是一个简单的例子:

<?php
$bits = 8 * PHP_INT_SIZE;
echo "(Info: This script is running as $bits-bit.)\r\n\r\n";

$connStr = 
        'odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};' .
        'Dbq=C:\Users\Gord\Desktop\foo.accdb;';

$dbh = new PDO($connStr);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = 
        "SELECT AgentName FROM Agents " .
        "WHERE ID < ? AND AgentName <> ?";
$sth = $dbh->prepare($sql);

// query parameter value(s)
$params = array(
        5,
        'Homer'
        );

$sth->execute($params);

while ($row = $sth->fetch()) {
    echo $row['AgentName'] . "\r\n";
}

NOTE:The above approach is sufficient if you do notneed to support Unicode characters above U+00FF. If you doneed to support such characters then neither PDO_ODBCnor the old odbc_functions will work; you'll need to use the solution described in this answer.

注:上述方法是足够的,如果你没有需要支持上述Unicode字符U+00FF。如果您确实需要支持这些字符,那么PDO_ODBCodbc_函数和旧函数都不起作用;您需要使用此答案中描述的解决方案。

回答by Joel Ginsberg

The problem is a simple typo. You named your variable 'conc' on line 2 but then referenced 'conn' on line 4.

问题是一个简单的错字。您在第 2 行将变量命名为“conc”,但随后在第 4 行引用了“conn”。

回答by Aman Maurya

<?php
    $dbName = $_SERVER["DOCUMENT_ROOT"] . "products\products.mdb";
    if (!file_exists($dbName)) {
       die("Could not find database file.");
    }
    $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

A successful connection will allow SQL commands to be executed from PHP to read or write the database. If, however, you get the error message “PDOException Could not find driver” then it's likely that the PDO ODBC driver is not installed. Use the phpinfo() function to check your installation for references to PDO.

成功的连接将允许从 PHP 执行 SQL 命令以读取或写入数据库。但是,如果您收到错误消息“PDOException 找不到驱动程序”,则可能是未安装 PDO ODBC 驱动程序。使用 phpinfo() 函数检查您的安装是否引用了 PDO。

If an entry for PDO ODBC is not present, you will need to ensure your installation includes the PDO extension and ODBC drivers. To do so on Windows, uncomment the line extension=php_pdo_odbc.dll in php.ini, restart Apache, and then try to connect to the database again.

如果 PDO ODBC 条目不存在,您需要确保您的安装包括 PDO 扩展和 ODBC 驱动程序。要在 Windows 上执行此操作,请取消注释 php.ini 中的行 extension=php_pdo_odbc.dll,重新启动 Apache,然后再次尝试连接到数据库。

With the driver installed, the output from phpinfo() should include information like this:https://www.diigo.com/item/image/5kc39/hdse

安装驱动程序后,phpinfo() 的输出应包含如下信息:https: //www.diigo.com/item/image/5kc39/hdse

http://i.stack.imgur.com/Zwp2W.png

http://i.stack.imgur.com/Zwp2W.png

回答by MSR

Are you sure the odbc connector is well created ? if not check the step "Create an ODBC Connection" again

您确定 odbc 连接器创建良好吗?如果没有再次检查步骤“创建 ODBC 连接

EDIT: Connection without DSN fromphp.net

编辑没有来自php.net 的DSN 的连接

// Microsoft Access

// 微软访问

$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

in your case it might be if your filename is northwind and your file extension mdb:

在您的情况下,可能是您的文件名是 Northwind 并且您的文件扩展名是 mdb:

$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=northwind", "", "");

回答by user13319093

回答by Hu3

If you are struggling with the connection in the XAMPP environment I suggest uncommenting the following entry in the php.inifile.

如果您在 XAMPP 环境中遇到连接问题,我建议取消注释文件中的以下条目php.ini

extension = odbc

extension = odbc

I received an error without it: Uncaught pdoexception: could not find driver

没有它我收到一个错误: Uncaught pdoexception: could not find driver