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选项以图表形式显示。

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()的输出应包含以下信息:

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数据库

php access数据库_在PHP中使用Access数据库相关推荐

  1. 怎么在Vc中打开MySQL数据库_在vc++中使用mysql数据库_MySQL

    vc++版本:v6.0 Host Info: localhost via TCP/IP Server Info: 5.0.67-community-nt Server Version: 50067 C ...

  2. uwp连接mysql数据库_在UWP中使用SQLite数据库,支持加密

    这个库支持Xamarin.*(iOS,Android等). .NET, .NET Core.Mono.UWP等主流系统和程序.支持对数据库文件的加密,其sqlcipher加密版为sqlite-net- ...

  3. echarts导入mysql数据库_Echarts最新:Django中从mysql数据库中获取数据传到echarts方式_爱安网 LoveAn.com...

    关于"Echarts"的最新内容 聚合阅读 这篇文章主要介绍了基于vue+echarts 数据可视化大屏展示的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随 ...

  4. java代码连接oracle数据库连接_在JAVA中连接Oracle数据库(例子)

    * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - java - 代码样式 - 代码模板 */ package person.fane.test; import java.sql ...

  5. 在mysql中更新数据sql语句怎么写_在MySQL中,更新数据库表记录的SQL语句,包括______语句...

    在MySQL中,更新数据库表记录的SQL语句,包括______语句 答:insert replace update delete 在域F中,设其特征为2,对于任意a,b∈F,则(a+b)2 等于多少 ...

  6. mysql 的新建查询怎么弄_在Navicat中如何新建数据库和表并做查询

    上一篇文章,小编给大家分享了在Navicat中如何远程连接数据库,没有来得及上车的小伙伴可以戳这篇文章:在Ubuntu14.04中配置mysql远程连接教程.今天小编给大家分享一下如何在Navicat ...

  7. 如何批量删除mysql的数据库_如何快速批量删除Mysql数据库中的数据表

    一个mysql数据库中,是可以同时安装几个网站程序的,在使用中,我们只需要用不同的数据库表前缀来区分就可以了.但随着我们不断调试,那么数据 库中的表,就会一天天的多起来,如一个一个的删除就很麻烦.很辛 ...

  8. mysql设置数据库显示汉字_在CMD中操作mysql数据库出现中文乱码解决方案

    百度了一下..有说将cmd字符编码用chcp命令改为65001(utf8字符编码),可这样之后根本无法输入中文,查询出的中问结果依旧乱码 . 其实,只要保证cmd客户端和MySQL两者编码一致即可. ...

  9. qt中的数据库可以创建在主函数中吗_在qt中怎么建立数据库

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

最新文章

  1. mysql count(*),count(1)与count(column)区别
  2. 制作 docker 镜像
  3. 读书和不读书有什么区别呢?
  4. MySQL建立双向主备复制server配置方法
  5. c#与mysql教程_C#连接MySQL操作详细教程
  6. php 高德地图经纬度,高德地图php 换取经纬度 地址
  7. Spark实践的阶段性总结
  8. dreamweavercc 数据库_Dreamweaver CC
  9. 二代测序方法:DNA测序之靶向重测序
  10. 用Python验证指数基金定投策略
  11. SPIR-V教程 -- SPIR-V介绍(一)
  12. 计算机辅助小学数学教学的研究,计算机辅助小学数学教学研究.doc
  13. 什么是单例模式以及单例模式的几种实现
  14. Spring Ioc原理解析
  15. Oracle ClusterwarePRCT-1011 : Failed to run oifcfg. Detailed error: null
  16. IA32-Linux地址转换过程
  17. 【GRU时序预测】基于matlab卷积神经网络结合门控循环单元CNN-GRU时间序列预测【含Matlab源码 2287期】
  18. MTK开发之—开机永不灭屏
  19. DOS 命令 批量注册指定文件下的所有DLL
  20. 面向对象:叶子的离开是风的追求还是树的不挽留?我若是飘离的叶子,你是否是温润我的春泥?

热门文章

  1. 在ubuntu下完美安装RTX(解决离线、乱码问题)
  2. 七年级上册计算机工作计划,七年级上学期班主任工作计划(精选3篇)
  3. web服务器理解和重要性
  4. LabVIEW扫描成像或同步模拟电压生成和数据采集
  5. 【程序源代码】微信爬虫
  6. Academic+Phrasebank+2021+Edition+_中英文对照 一共314页 2021最新版PDF已经翻译好便于阅读对照
  7. BFS 之Flood Fill 算法
  8. 阿迪达斯联合贝克汉姆推出ULTRABOOST 19 DB99跑鞋
  9. 比较好的Java 网站/论坛/博客集锦
  10. 无法进入系统如何正常备份数据