如何将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驱动程序管理器]找不到数据源名称且未指定默认驱动程序,SQLConnect中C:\ Users \ NNALI中的SQL状态IM002第2行上的\ Desktop \ root \ test.php
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_exec()期望参数1为资源,在第4行的C:\ Users \ NNALI \ Desktop \ Breweries \ root \ test.php中给出布尔值
Warning: odbc_exec() expects parameter 1 to be resource, boolean given in C:\Users\NNALI\Desktop\Breweries\root\test.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.)\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";
}
注意::如果您不需要需要支持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屋!