在PHP网站开发中,PHP与Mysql是最好的组合,但是当你想将其他平台的网站移植到PHP平台时,必然遇到移植性的问题,如ASP+ACCESS平台如何移植?首当其冲便是PHP连接Access数据库问题,在不改变数据库的情况下,PHP如何与Access数据库建立连接?
PHP提供多种连接数据库解决方案,在此详解如何使用PHP ADOdb、PDO、ODBC与Access数据库建立连接的代码实例,作为抛砖引玉。
准备工作
使用OFFICE工具建立Access数据库文件
一、使用PHP ADOdb连接Access数据库
1、首先你需要安装PHP ADOdb类库。
2、使用PHP ADOdb连接Access数据库代码如下
1 |
|
说明:与使用PHP ADOdb与Mysql数据库建立连接类似,首先把ADOdb类库包含进来,然后调用ADONewConnection、Connect、Execute与Access数据库建立连接并执行查询操作。
二、使用PHP PDO连接Access数据库
PDO功能需PHP5以上支持,在使用PDO之前你必须确保安装了PDO功能,如何配置安装PDO呢?
只要在PHP.INI配置文件中找到extension_dir,使之指向扩展库目录地址,同时去除你想要使用的PDO驱动DLL之前的分号(;),重启Apache,PDO就算安装好了。这里由于我们使用PDO连接Access数据库,所以至少确保php_pdo.dll,php_pdo_odbc.dll能支持。
使用PDO连接Access数据库代码实例
1 |
|
说明:首先初始化PDO对象,建立PHP与Access数据库之间的连接,然后通过PDO query函数执行查询操作。
三、使用ODBC连接Access数据库
使用ODBC连接Access数据库代码实例
1 |
|
说明:首先使用odbc_connect连接access数据库,前三个参数分别为:$DSN,数据库用户名,密码,第四个参数设置为SQL_CUR_USE_ODBC主要是为了避免连接Access数据库发生意外错误;然后使用odbc_do执行查询操作,并调用odbc_fetch_row、odbc_result输出查询内容,最后使用odbc_close关闭Access数据库连接。
至此使用PHP ADOdb、PDO、ODBC连接Access数据库并进行操作的代码实例就介绍完了,通过上述实例,我们可以看到其实PHP连接Access数据库的方法大同小异,使用哪种方法取决于PHP环境的配置情况。
注:PHP网站开发教程-leapsoul.cn版权所有,转载时请以链接形式注明原始出处及本声明,谢谢。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
