如何将 PHP 与 Microsoft Access 数据库连接 [英] How to connect PHP with Microsoft Access database
问题描述
我目前面临着一个新的挑战,即使用 Microsoft Access 而不是 mysql 作为主数据库来开发站点.我以前没有使用过 MS Access,我想了解如何使用它的指导,我在 W3schools 但代码给出了错误
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 W3schools but the code gives error
警告:odbc_connect() [function.odbc-connect]:SQL 错误:[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序,C:UsersNNALI 中 SQLConnect 中的 SQL 状态为 IM002Desktop oot est.php 第 2 行
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:UsersNNALIDesktop oot est.php on line 2
还有这个错误
警告:odbc_exec() 期望参数 1 是资源,布尔值在 C:UsersNNALIDesktopBreweries oot est.php 第 4 行中给出
Warning: odbc_exec() expects parameter 1 to be resource, boolean given in C:UsersNNALIDesktopBreweries oot est.php on line 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);
?>
以上是我使用的代码
推荐答案
如果您刚刚开始一个新项目,那么我建议您使用 PDO 而不是旧的 odbc_exec()
方法.这是一个简单的例子:
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:
<?php
$bits = 8 * PHP_INT_SIZE;
echo "(Info: This script is running as $bits-bit.)
";
$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'] . "
";
}
注意:如果您不需要支持U+00FF
以上的Unicode字符,上述方法就足够了.如果您确实需要支持这些字符,那么PDO_ODBC
和旧的odbc_
函数都将无法工作;您需要使用此答案中描述的解决方案.
NOTE: The above approach is sufficient if you do not need to support Unicode characters above U+00FF
. If you do need to support such characters then neither PDO_ODBC
nor the old odbc_
functions will work; you'll need to use the solution described in this answer.
这篇关于如何将 PHP 与 Microsoft Access 数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!