我目前面临着使用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
而这个错误
Warning: odbc_exec() expects parameter 1 to be resource, boolean given in C:\Users\NNALI\Desktop\Breweries\root\test.php on line 4
我被困住了,不知道该怎么做,我将非常感谢你们的帮助.
<?php
$conc = odbc_connect("northwind", "","");
$sql = "Select * From customers";
$rs = odbc_exec($conn, $sql);
?>
以上是我使用的代码
解决方法:
如果您刚开始使用新项目,那么我建议您使用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";
}