php access数据库_在PHP中使用Access数据库

php access数据库

A previous client of mine had an Access database of products they sell, which they had been using offline. But increasingly this working arrangement had proven to be limited. There had been too many instances when they needed to use the database but weren’t near the computer where they kept their database.

我以前的一个客户拥有他们所销售产品的Access数据库,这些数据库一直在离线使用。 但是事实证明,这种工作安排越来越有限。 他们需要使用数据库的实例太多了,但是它们不在存放数据库的计算机附近。

Wouldn’t it have been better for them to able to maintain their database where ever they were? Of course, but how? Simple – put it online. In addition, having their product database online opens the door to using it as the basis of a website for their business.

对他们来说,能够在任何地方维护自己的数据库会更好吗? 当然可以,但是如何呢? 简单–在线。 此外,将他们的产品数据库在线可以打开将其用作其业务网站的基础的大门。

There’s another advantage to having an online database, but which is a significant – multi-user access. That is: storing the database on a server means that any authorized person be able to use the company’s product database using nothing more than a browser. And all as a result of moving an existing Access database file from an offline to an online location.

拥有在线数据库还有另一个优势,但这很重要–多用户访问。 也就是说:将数据库存储在服务器上意味着任何授权人都可以使用浏览器来使用公司的产品数据库。 以及所有将现有Access数据库文件从脱机位置移动到联机位置的结果。

To make the transition to online databases easier, the existing Access database can be kept unchanged and simply uploaded to a suitable Windows host. (Linux hosting is possible too, but that’s slightly more complicated.) The hosting is not enough in itself, though, because you also need to build an interface allowing them to read and write to the database. Building an interface may sound daunting, but it needn’t be. In effect, what you’ll be doing is replacing the forms listed under the Forms tab in Access.

为了使向在线数据库的过渡更加容易,可以将现有的Access数据库保持不变,而只需将其上传到合适的Windows主机即可。 (也可以使用Linux托管,但是稍微复杂一些。)托管本身本身还不够,因为您还需要构建一个允许它们读写数据库的接口。 建立一个接口听起来可能令人生畏,但不是必须的。 实际上,您要做的是替换Access中“表单”选项卡下列出的表单。

In this article I’ll focus on the essential elements of PHP you’ll need to use an existing Access database online. One other item that’s standard with a Windows installation is the availability of ODBC drivers. These are essential for the use of Access from PHP.

在本文中,我将重点介绍PHP的基本要素,您需要在线使用现有的Access数据库。 Windows安装的另一项标准是ODBC驱动程序的可用性。 这些对于使用PHP中的Access是必不可少的。

前端和后端 (Front-end and Back-end)

First of all, let’s clarify how the client’s Access database would be opened. Normally they’d use Microsoft Access on their PC, perhaps by double-clicking the icon for the database file. With the database online, however, they will be opening the database in a browser.

首先,让我们阐明如何打开客户端的Access数据库。 通常,他们可能会在PC上使用Microsoft Access,可能是双击数据库文件的图标。 但是,随着数据库在线,他们将在浏览器中打开数据库。

How can a browser be used to open, view, and edit an Access database? Well, think of the MS Access application as a user interface. As a UI, it has been designed to be used within Windows to read and write an Access database file. Remember, an Access database file is completely separate from the MS Access application software. Thus it is possible to build an alternative UI to read and write to an Access database file. This one just happens to be run using a web browser.

如何使用浏览器打开,查看和编辑Access数据库? 好吧,将MS Access应用程序视为用户界面。 作为UI,已将其设计为在Windows中用于读取和写入Access数据库文件。 请记住,Access数据库文件与MS Access应用程序软件完全分开。 因此,可以构建一个替代UI来读取和写入Access数据库文件。 这只是碰巧是使用Web浏览器运行的。

Before getting into the nuts and bolts of the UI, it is important to keep in mind that the data and the user interface are distinct aspects of a system. Let’s take a moment to reinforce a key aspect of using a browser to access a database hosted on a server.

在深入了解UI之前,请务必牢记数据和用户界面是系统的不同方面。 让我们花一点时间来强调使用浏览器访问服务器上托管的数据库的关键方面。

When using MS Access on a PC to use an Access database (that is, offline), the application software is the front-end and the database file (.mdb) is the back-end. Similarly, when going online, the browser is the front-end and the database file is the back-end. In other words, the relationship hasn’t changed: it is still a front-end/back-end relationship. The front-end connects to the back-end, and the back-end stores the data.

在PC上使用MS Access来使用Access数据库(即脱机)时,应用程序软件是前端,而数据库文件( .mdb )是后端。 同样,在联机时,浏览器是前端,而数据库文件是后端。 换句话说,关系没有改变:它仍然是前端/后端关系。 前端连接到后端,后端存储数据。

数据库 (The Database)

In the example I’ve chosen for this article, we’ll use a very simple database that consists of three tables: a product table, a category table, and a linking product-category table.

在我为本文选择的示例中,我们将使用一个非常简单的数据库,该数据库包含三个表:一个产品表,一个类别表和一个链接的产品类别表。

The Microsoft Access database contains all the data and table definitions that are relevant to this article. As per best practice, the data is separated into distinct tables. For example, the list of products is stored in the product table, and the list of categories is stored in the category table. The Access database is self-contained, with no links to external tables.

Microsoft Access数据库包含与本文相关的所有数据和表定义。 按照最佳实践,数据被分为不同的表。 例如,产品列表存储在product表中,类别列表存储在category表中。 Access数据库是独立的,没有指向外部表的链接。

Many products can be of any given category, and any given product can belong to more than one category. For example, if the database purpose is to store details of silver antiques, a pair of 19th-Century silver cuff-links can be categorised as both 19th-Century and Gifts for Men.

许多产品可以属于任何给定类别,并且任何给定产品可以属于多个类别。 例如,如果数据库的目的是存储银色古董的详细信息,则可以将一对19世纪的银袖扣分类为19世纪和男士礼品。

This relationship between the data is shown diagrammatically using Access’s own Relationships option.

数据之间的这种关系使用Access自己的Relationships选项以图表形式显示。

alt

That is, there is a many-to-many relationship between products and categories. Hence the need for the link table.

也就是说,产品和类别之间存在多对多关系。 因此需要链接表。

连接数据库 (Connection to Database)

Before reading or writing a database, a connection must be made to it. Windows hosts have a similar folder hierarchy to a Windows PC, so the location of the files on a Windows server will likely have a path of the form C:inetpubvhostsexample.comhttpdocs. You should be able to extract the value of the path to the root folder of your host using the PHP superglobal $_SERVER["DOCUMENT_ROOT"]. The code needed to connect to the Access database then would be similar to:

在读取或写入数据库之前,必须先建立连接。 Windows主机的文件夹层次结构与Windows PC相似,因此Windows服务器上文件的位置可能具有C:inetpubvhostsexample.comhttpdocs形式的路径。 您应该能够使用PHP超全局性$_SERVER["DOCUMENT_ROOT"]提取主机根目录的路径值。 然后,连接到Access数据库所需的代码将类似于:

<?php
$dbName = $_SERVER["DOCUMENT_ROOT"] . "products\products.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");

A successful connection will allow SQL commands to be executed from PHP to read or write the database. If, however, you get the error message “PDOException Could not find driver” then it’s likely that the PDO ODBC driver is not installed. Use the phpinfo() function to check your installation for references to PDO.

成功的连接将允许从PHP执行SQL命令以读取或写入数据库。 但是,如果收到错误消息“ PDOException无法找到驱动程序”,则可能未安装PDO ODBC驱动程序。 使用phpinfo()函数检查您的安装中是否有对PDO的引用。

If an entry for PDO ODBC is not present, you will need to ensure your installation includes the PDO extension and ODBC drivers. To do so on Windows, uncomment the line extension=php_pdo_odbc.dll in php.ini, restart Apache, and then try to connect to the database again.

如果不存在用于PDO ODBC的条目,则需要确保您的安装包括PDO扩展名和ODBC驱动程序。 要在Windows上执行此操作,请取消注释php.ini extension=php_pdo_odbc.dll行,重新启动Apache,然后尝试再次连接到数据库。

With the driver installed, the output from phpinfo() should include information like this:

安装驱动程序后, phpinfo()的输出应包含以下信息:

PDO_ODBC in phpinfo() output

SQL命令 (SQL Commands)

The four basic commands used from PHP are the same as those used within MS Access, namely SELECT, UPDATE, INSERT, and DELETE. Thus, the following SQL examples should be easy to follow. Unless, that is, you normally use Access’s Design View, in which you may need an SQL refresher course.

PHP中使用的四个基本命令与MS Access中使用的四个命令相同,即SELECTUPDATEINSERTDELETE 。 因此,以下SQL示例应易于理解。 除非是,除非您通常使用Access的设计视图,否则您可能需要其中SQL复习课程。

SELECT行 (SELECT row(s))

Suppose you need to get the price for a specific product. This is readily achieved using the id of the product to select a single field from the product table.

假设您需要获取特定产品的价格。 使用产品的ID从product表中选择单个字段即可轻松实现。

<?php
$sql  = "SELECT price FROM product";
$sql .= " WHERE id = " . $productId;

$result = $db->query($sql);
$row = $result->fetch();

$productPrice = $row["price"];

After choosing a category from a pull-down list, you can use the category id to query the link table to obtain a list of products that have been assigned to that category.

从下拉列表中选择类别后,可以使用类别ID查询链接表,以获得已分配给该类别的产品的列表。

<?php
$sql  = "SELECT p.name, p.description, p.price";
$sql .= "  FROM product p, product_category pc";
$sql .= " WHERE p.id  = pc.productId";
$sql .= "  AND pc.category_id = " . $categoryId;
$sql .= " ORDER BY name";

$result = $db->query($sql);
while ($row = $result->fetch()) {
    $productName        = $row["name"];
    $productDescription = $row["description"];
    $productPrice       = $row["price"];
}

UPDATE行 (UPDATE row)

Using details provided by an HTML form, you can update a product’s details.

使用HTML表单提供的详细信息,您可以更新产品的详细信息。

<?php
$sql  = "UPDATE product";
$sql .= "   SET description = " . $db->quote($strDescription) . ",";
$sql .= "       price       =  " . $strPrice . ",";
$sql .= "       sale_status = " . $db->quote($strDescription);
$sql .= " WHERE id = " . $productId;

$db->query($sql);

A troublesome character to be wary of is the apostrophe. The apostrophe can’t be stored in an Access table simply as an apostrophe, rather it has to be escaped by another apostrophe. This ensures that Access knows it is to be stored as an apostrophe and not as the apostrophe that delimits the string. Fortunately, PHP’s PDO library contains a method that prepares strings for storing in a database, quote().

要警惕的麻烦人物是撇号。 撇号不能简单地作为撇号存储在Access表中,而是必须由另一个撇号转义。 这样可以确保Access知道将其存储为撇号而不是作为分隔字符串的撇号。 幸运的是,PHP的PDO库包含一个准备用于存储在数据库中的字符串的方法quote()

插入行 (INSERT row)

You can add a new product to the product table, using details entered in an HTML form.

您可以使用在HTML表单中输入的详细信息将新产品添加到product表中。

<?php
$sql  = "INSERT INTO product";
$sql .= "       (name, description, price, sale_status) ";
$sql .= "VALUES (" . $db->quote($strName) . ", " . $db->quote($strDescription) . ", " . $strPrice . ", " . $db->quote($strStatus) . ")";

$db->query($sql);

删除行 (DELETE row)

If a product has been entered by mistake, or is no longer needed in the database, you can delete it from the product table.

如果错误输入了产品,或者数据库中不再需要该product ,则可以从product表中将其删除。

<?php
$sql  = "DELETE";
$sql .= "  FROM product";
$sql .= " WHERE id = " . $productId;

$db->query($sql);

It could be argued that deleting a product is unlikely as it’s better to have a suitable status to indicate a product has been sold or has been archived, etc. Thus, the details would be retained for future reference.

可能会争辩说,删除产品是不太可能的,因为最好具有适当的状态以指示产品已售出或已存档等。因此,将保留详细信息以备将来参考。

摘要 (Summary)

The above is the bare bones of what is needed to use an Access database from PHP. I hope it shows how little PHP is needed for the essential nitty-gritty of reading and writing an Access database, and how easy it is to understand these basic elements of database interaction using PHP.

以上是使用PHP中的Access数据库所需的基本知识。 我希望它能显示基本的读写Access数据库所需PHP数量,以及理解使用PHP进行数据库交互的这些基本元素有多么容易。

Reviewing the above SQL, it’s clear there were only three PDO ODBC commands required to allow PHP to access Access, namely, $db = new PDO(), $db->query(), $db->query(), and $db->quote(). Of course, this is the bare minimum. More complicated databases, and more complicated websites, will require more SQL than shown in this article, but you have now the basics to put your own – or your client’s – Access database online using PHP.

回顾上面SQL,很明显,只需要三个PDO ODBC命令即可允许PHP访问Access,即$db = new PDO()$db->query()$db->query()$db->quote() 。 当然,这是最低要求。 比本文中所示,更复杂的数据库和更复杂的网站将需要更多SQL,但是您现在已经具备使用PHP在线访问自己的(或客户的)访问数据库的基础知识。

Comments on this article are closed. Have a question about PHP? Why not ask it on our forums?

本文的评论已关闭。 对PHP有疑问吗? 为什么不在我们的论坛上提问呢?

翻译自: https://www.sitepoint.com/using-an-access-database-with-php/

php access数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值