search
HomeBackend DevelopmentPHP Tutorialphp 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
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
How can you check if a PHP session has already started?How can you check if a PHP session has already started?Apr 30, 2025 am 12:20 AM

In PHP, you can use session_status() or session_id() to check whether the session has started. 1) Use the session_status() function. If PHP_SESSION_ACTIVE is returned, the session has been started. 2) Use the session_id() function, if a non-empty string is returned, the session has been started. Both methods can effectively check the session state, and choosing which method to use depends on the PHP version and personal preferences.

Describe a scenario where using sessions is essential in a web application.Describe a scenario where using sessions is essential in a web application.Apr 30, 2025 am 12:16 AM

Sessionsarevitalinwebapplications,especiallyfore-commerceplatforms.Theymaintainuserdataacrossrequests,crucialforshoppingcarts,authentication,andpersonalization.InFlask,sessionscanbeimplementedusingsimplecodetomanageuserloginsanddatapersistence.

How can you manage concurrent session access in PHP?How can you manage concurrent session access in PHP?Apr 30, 2025 am 12:11 AM

Managing concurrent session access in PHP can be done by the following methods: 1. Use the database to store session data, 2. Use Redis or Memcached, 3. Implement a session locking strategy. These methods help ensure data consistency and improve concurrency performance.

What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function