PHP 和 Microsoft Access 数据库 - 连接和 CRUD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1605473/
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
PHP and Microsoft Access database - Connection and CRUD
提问by Mask
I have no experience with access.
我没有访问经验。
How to do update/insert/delete/select statement with and without $rs = new com("ADODB.RecordSet");?
如何使用和不使用更新/插入/删除/选择语句$rs = new com("ADODB.RecordSet");?
回答by sepehr
PDO
PDO
If you want to interface with an MS Access database using PHP, PDOis available for you.
如果您想使用 PHP 与 MS Access 数据库进行交互,则可以使用PDO。
<?php
try {
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin");
}
catch (PDOException $e) {
echo $e->getMessage();
}
When using PDO, due to the unified interface for DB operations, you have the opportunity to make your app more portable across various RDBMs systems. All you have to do is to provide the connection string to the PDO new instance and have the correct PDO driver installed.
使用 PDO 时,由于 DB 操作的统一接口,您有机会使您的应用程序在各种 RDBM 系统之间更具可移植性。您所要做的就是向 PDO 新实例提供连接字符串并安装正确的 PDO 驱动程序。
As the result of this unified interface, your application can be easily ported from MS Access to MySQL, SQLite, Oracle, Informix, DB2, etc. which most certainly is the case if it ages enough.
由于这个统一的界面,您的应用程序可以轻松地从 MS Access 移植到 MySQL、SQLite、Oracle、Informix、DB2 等。如果它足够老,肯定是这种情况。
Here's an insertion example:
这是一个插入示例:
<?php
try {
// Connect,
// Assuming that the DB file is available in `C:\animals.mdb`
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");
// INSERT data
$count = $pdo->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");
// echo the number of affected rows
echo $count;
// close the database connection
// See: http://php.net/manual/en/pdo.connections.php
$pdo = null;
}
catch (PDOException $e) {
echo $e->getMessage();
}
ODBC
ODBC
In case you don't want to use PDO for some insane reasons, you can look into ODBC.
如果您出于某些疯狂的原因不想使用 PDO,您可以查看ODBC。
Here's an example:
下面是一个例子:
<?php
if (! $conn = odbc_connect('northwind', '', '')) {
exit("Connection Failed: $conn");
}
if (! $rs = odbc_exec($conn, 'SELECT * FROM customers')) {
exit('Error in SQL');
}
while (odbc_fetch_row($rs)) {
echo 'Company name: ', odbc_result($rs, 'CompanyName'), PHP_EOL;
echo 'Contact name: ', odbc_result($rs, 'ContactName'), PHP_EOL;
}
odbc_close($conn);

