PHP 和 Microsoft Access 数据库 - 连接和 CRUD [英] PHP and Microsoft Access database - Connection and CRUD
问题描述
我没有访问经验.
如何使用和不使用$rs = new com("ADODB.RecordSet");
?
推荐答案
PDO
如果您想使用 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();
}
在使用 PDO 时,由于 DB 操作的统一接口,您有机会使您的应用程序在各种 RDBM 系统之间更具可移植性.您所要做的就是向 PDO 新实例提供连接字符串并安装正确的 PDO 驱动程序.
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.
这个统一接口的结果是,您的应用程序可以轻松地从 MS Access 移植到 MySQL、SQLite、Oracle、Informix、DB2 等.如果它的年龄足够大,情况肯定是这样.
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.
这是一个插入示例:
<?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
如果您出于某些疯狂的原因不想使用 PDO,您可以查看 ODBC.
这是一个例子:
<?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);
这篇关于PHP 和 Microsoft Access 数据库 - 连接和 CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!