Home Backend Development PHP Tutorial php access 数据连接与读取保存编辑数据的实现代码_php技巧

php access 数据连接与读取保存编辑数据的实现代码_php技巧

May 17, 2016 am 09:25 AM
access php

复制代码 代码如下:

$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("www.jb51.net/db.mdb");
//与access连接要用到com接口了。
$conn->Open($connstr);
$rs = new com("ADODB.RecordSet");
//数据查询并显示出来
$rs->Open("select * from szd_t",$conn,1,1);
while(! $rs->eof) {
$f = $rs->Fields(1);
echo $f->value;
$rs->MoveNext();
}
//下面来看一下php access数据库教程保存
$sql ="insert into szd_t(title)values('www.jb51.net')";
$rs->Open( $sql );
echo '保存成功';
//php access数据库编辑
$sql ="Update szd_t set title='jb51.net' where id=".$_GET['id'];
$rs->Open( $sql );
echo '编辑成功';
//删除数据
$sql ="delete from szd_t where id=".$_GET['id'];


php连接Access数据库的三种方法
最近想把一个asp的网站改成php的,无奈空间不支持mysql数据库,只好用access数据库了,但以前都是用的php+mysql,php+access数据库编程还真没有做过.
感谢党,感谢cctv,感谢搜索引擎,这不找到一编不错的文章,特转过来和大家分享.
在PHP中连接Access有如下3种方式。
(1)创建系统数据源,用PHP提供的ODBC函数即可。
(2)同样可以使用PHP的ODBC函数,但不创建数据源。
开放数据库连接(Open DateBase Conection,ODBC)是Windows Open Server(开放服务)API(WOSA)产品之一。一个数据源是对数据库的一个命名连接。对于应用程序要连接的不同类型的数据库,都需要一个ODBC驱动程序。ODBC API主要是为客户/服务器的RDBMS使用设计的,但是ODBC驱动程序也可以用于连接桌面数据库文件、工作表和平面文件。ODBC使用Odbcinst.dll库来设置和清除数据源。Odbcad32.exe是一个用于建立ODBC数据源的独立的32位可执行应用程序,在控制面板中有其对应的图标Control Panel。
ODBC驱动管理程序为数据源打开ODBC驱动程序并将SQL语句传送给驱动程序。在客户/服务器RDBMS处理完一个select查询后,ODBC驱动程序将值返回给应用程序。当执行一个insert、update或delete语句时,驱动程序返回查询所影响的行数。 phperz.com
下面介绍PHP使用ODBC连接Access数据库的方法。用$connstr="DRIVER= Microsoft Access Driver (*.mdb)来设置数据驱动,函数realpath()用来取得数据库的相对路径。利用该方法连接Access数据库主要应用到PHP的odbc_connect()函数,该函数声明如下: www.phperz.com
复制代码 代码如下:

resourse odbc_connect( string dsn, string user, string password [, int cursor_type])
dsn:系统dsn名称。
user:数据库服务器某用户名。
password:数据库服务器某用户密码。
cursor_type:游标类型。

代码如下:
复制代码 代码如下:

$connstr="DRIVER=Microsoft Access Driver (*.mdb);
DBQ=".realpath("bookinfo.mdb");
$connid=odbc_connect($connstr,"","",SQL_CUR_USE_ODBC );

(3)使用微软的ADODB数据库驱动。ActiveX Data Objects(ADO)是Microsoft开放数据库应用程序的数据库访问技术。它被设计用来同新的数据访问层OLE DB Provider一起协同工作,提供通用数据访问(Universal Date Access)。OLE DB是一个低层的数据访问接口,用它可以访问各种数据源,包括传统的关系型数据库、电子邮件系统及自定义的商业对象。ADO技术大大简化了OLE DB的操作,因为ADO封装了OLE DB程序中使用的大量COM接口,所以ADO是一种高层的访问技术。 php程序员站
ADO技术基于通用对象模型(COM),它提供了多种语言的访问技术。PHP是通过预先定义类COM来使用ADO方法操纵Access数据库的。该类详细说明如下: www.phperz.com
复制代码 代码如下:

string com::com( string module_name [, string server_name [, int codepage]])
module_name:被请求组件的名字或class-id。 www~phperz~com
server_name:DCOM服务器的名字。
Codepage:指定用于将PHP字符串转换成UNICODE字符串的代码页,反之亦然。该参数的取值有CP_ACP、CP_MACCP、CP_OEMCP、CP_SYMBOL、CP_THREAD_ACP、CP_UTF7和CP_UTF8。

PHP利用com类并使用ADO方法访问数据库的代码如下:
[code]
$conn = new com("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("bookinfo.mdb ");
$conn->Open($connstr);


这篇是别的网友发的一篇文章。结合下,最后脚本之家会给出一个php+access的留言本源码,大家可以参考下。基本上对php access的操作就熟悉了。
虽然很少用PHP链接ACCESS,但偶尔用来导导数据,还是挺不错的
复制代码 代码如下:

/*
创建ADO连接
*/
$conn = @new COM("ADODB.Connection") or die ("ADO Connection faild.");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("DATUM/cnbt.mdb");
$conn->Open($connstr);
/*
创建记录集查询
*/
$rs = @new COM("ADODB.RecordSet");
$rs->Open("select * from dbo_dirs",$conn,1,3);
/*
循环读取数据
*/
while(!$rs->eof){
echo "$rs->Fields["title"]->Value;
echo "
";
$rs->Movenext(); //将记录集指针下移
}
$rs->close();
?>


函数描述及例子
虽然很少用PHP链接ACCESS,但偶尔用来导导数据,还是挺不错的

PHP ACCESS 简单留言本实例源码
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles