PHP中ADODB如何操作access数据库?
这篇主要介绍了PHP中ADODB如何操作access数据库,有一定的参考价值,感兴趣的朋友可以参考一下!
<?php //定义数据库变量 $DB_TYPE = "mysql"; $DB_HOST = "localhost"; $DB_USER = "root"; $DB_PASS = ""; $DB_DATABASE = "ai-part"; require_once("../adodb/adodb.inc.php"); $db = NewADOConnection("$DB_TYPE");//建立数据库对象 $db->debug = true;//数据库的DEBUG测试,默认值是false $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;//返回的记录集形式,关联形式 /*** 返回的记录集形式 define('ADODB_FETCH_DEFAULT',0); define('ADODB_FETCH_NUM',1); define('ADODB_FETCH_ASSOC',2); define('ADODB_FETCH_BOTH',3); 以上常量,在adodb.inc.php里定义了,也就是可用"$ADODB_FETCH_MODE=2"方式 ADODB_FETCH_NUM 返回的记录集中的索引,是数字形式,即数据库字段的排序顺序值 ADODB_FETCH_ASSOC 返回的记录集中的索引,是原数据库字段名 ADODB_FETCH_BOTH 和 ADODB_FETCH_DEFAULT 是同时返回以上两种。某些数据库不支持 An example: $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $rs1 = $db->Execute('select * from table'); $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC; $rs2 = $db->Execute('select * from table'); print_r($rs1->fields); # 返回的数组是: array([0]=>'v0',[1] =>'v1') print_r($rs2->fields); # 返回的数组是: array(['col1']=>'v0',['col2'] =>'v1') ***/ //连接数据库,方法有Connect,PConnect,NConnect,一般使用Connect if (!@$db->Connect("$DB_HOST", "$DB_USER", "$DB_PASS", "$DB_DATABASE")) { exit('<a href="/">服务器忙,请稍候再访问</a>'); } /* $db-> $rs-> 此类的使用方法 Execute($sql),执行参数中的$sql语句 SelectLimit($sql,$numrows=-1,$offset=-1) $numrows:取几条记录,$offset,从第几条开始取,一般是用于分页,或只取出几条记录的时候用 */ //Example: 取出多个记录 $sql = "Select * FROM table orDER BY id DESC"; if (!$rs = $db->Execute($sql)) {//执行SQL语句,并把结果返回给$rs变量 echo $db->ErrorMsg();//这个是打印出错信息 $db->Close();//关闭数据库 exit(); } while (!$rs->EOF) {//遍历记录集 echo $rs->fields['username'] . '<br>'; //print_r($rs->fields)试试,$rs->fields['字段名'],返回的是这个字段里的值 $rs->MoveNext();//将指针指到下一条记录,否则出现死循环! } $rs->Close();//关闭以便释放内存 //插入新记录 $sql = "Insert table (user_type,username) VALUES (3, 'liucheng')"; $db->Execute($sql); //更新记录 $sql = "Update table SET user_type=3 Where id=2"; $db->Execute($sql); //删除记录 $sql = "Delete FROM table Where id=2"; $db->Execute($sql); // 取单个记录 //$db->GetRow($sql), 取第一条记录,并返回一个数组,出错返回false $sql = "Select username,password,user_type FROM table Where id=3"; $data_ary = $db->GetRow($sql); if ($data_ary == false) { echo '没有找到此记录'; exit(); } else { echo $data_ary['username'] . ' ' . $data_ary['password'] . ' ' . $data_ary['user_type'] . '<br>'; } //另一种方法 $sql = "Select username,password,user_type FROM table Where id=3"; if (!$rs = $db->Execute($sql)) { echo $db->ErrorMsg(); $db->Close(); exit(); } if (!$result = $rs->FetchRow()) { echo '没有找到此记录'; exit(); } else { echo $result['username'] . ' ' . $result['password'] . ' ' . $result['user_type'] . '<br>'; } // 取单个字段 //$db->GetOne($sql) 取出第一条记录的第一个字段的值,出错则返回false $sql = "Select COUNT(id) FROM table"; $record_nums = $db->GetOne($sql); echo $record_nums; $sql = "Select username,password,user_type FROM table Where user_id=1"; $result = $db->GetOne($sql); echo $result;//打印出username的值 /* 在进行添加,修改,删除记录操作时, 要对字符串型的字段,使用$db->qstr()对用户输入的字符进行处理, 对数字型字段,要进行数据判断 更新记录,注意:这是针对php.ini中,magic_quotes被设置为Off的情况,如果不确定,可以使用 $db->qstr($content,get_magic_quotes_gpc()) 注意:content= 等号右边,没有单引号 */ $sql = "Update table SET content=" . $db->qstr($content) . " Where id=2"; $db->Execute($sql); /*$db->Insert_ID(),无参数,返回刚刚插入的那条记录的ID值,仅支持部分数据库,带auto-increment功能的数据库,如PostgreSQL, MySQL 和 MS SQL */ //Example: $sql = "Insert table (user_type,username) VALUES (3, 'liucheng')"; $db->Execute($sql); $data_id = $db->Insert_ID(); echo $data_id; /*$db->GenID($seqName = 'adodbseq',$startID=1),产生一个ID值.$seqName:用于产生此ID的数据库表名,$startID:起始值,一般不用设置,它会把$seqName中的值自动加1.支持部分数据库,某些数据库不支持 Insert_ID,GenID,一般我用GenID,使用它的目的,是在插入记录后,要马上得到它的ID时,才用 */ /*Example: 先创建一个列名为user_id_seq的表,里面只有一个字段,id,int(10),NOT NULL,然后插入一条值为0的记录 */ $user_id = $db->GenID('user_id_seq'); $sql = "Insert table (id, user_type,username) VALUES (" . $user_id . ", 3, 'liucheng')"; $db->Execute($sql); /* $rs->RecordCount(),取出记录集总数,无参数 它好像是把取出的记录集,用count()数组的方法,取得数据的数量 如果取大量数据,效率比较慢,建议使用SQL里的COUNT(*)的方法 $sql = "Select COUNT(*) FROM table", 用此方法时,不要在SQL里加ORDER BY,那样会降低执行速度 Example: */ $sql = "Select * FROM table orDER BY id DESC"; if (!$rs = $db->Execute($sql)) { echo $db->ErrorMsg(); $db->Close(); exit(); } $record_nums = $rs->RecordCount(); /* 如果想对某一结果集,要进行两次同样的循环处理,可以用下面方法 以下,只是一个例子,只为说明$rs->MoveFirst()的使用方法 */ $sql = "Select * FROM table orDER BY id DESC"; if (!$rs = $db->Execute($sql)) { echo $db->ErrorMsg(); $db->Close(); exit(); } $username_ary = array(); while (!$rs->EOF) { $username_ary[] = $rs->fields['username'] echo $rs->fields['username'] . '<br>';//print_r($rs->fields)试试,$rs->fields['字段名'],返回的是这个字段里的值 $rs->MoveNext();//将指针指到下一条记录,不用的话,会出现死循环! } $username_ary = array_unique($username_ary); $rs->MoveFirst();//将指针指回第一条记录 while (!$rs->EOF) { echo $rs->fields['password'] . '<br>';//print_r($rs->fields)试试,$rs->fields['字段名'],返回的是这个字段里的值 $rs->MoveNext();//将指针指到下一条记录 } $rs->Close(); //当本页程序,对数据库的操作完毕后,要$db->Close(); $db->Close(); /*一个不错的方法 */ if (isset($db)) { $db->Close(); } ?>
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通
3. bootstrap教程

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

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