Home php教程 php手册 PHP中ADODB如何操作access数据库?

PHP中ADODB如何操作access数据库?

Jun 13, 2016 pm 12:30 PM
php

这篇主要介绍了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(&#39;ADODB_FETCH_DEFAULT&#39;,0);      
define(&#39;ADODB_FETCH_NUM&#39;,1);      
define(&#39;ADODB_FETCH_ASSOC&#39;,2);      
define(&#39;ADODB_FETCH_BOTH&#39;,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(&#39;select * from table&#39;);       
    $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;       
    $rs2 = $db->Execute(&#39;select * from table&#39;);       
    print_r($rs1->fields); # 返回的数组是: array([0]=>&#39;v0&#39;,[1] =>&#39;v1&#39;)       
    print_r($rs2->fields); # 返回的数组是: array([&#39;col1&#39;]=>&#39;v0&#39;,[&#39;col2&#39;] =>&#39;v1&#39;)       
***/       //连接数据库,方法有Connect,PConnect,NConnect,一般使用Connect        if (!@$db->Connect("$DB_HOST", "$DB_USER", "$DB_PASS", "$DB_DATABASE")) {        
    exit(&#39;<a href="/">服务器忙,请稍候再访问</a>&#39;);        
}        
/*      $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[&#39;username&#39;] . &#39;<br>&#39;;        
      //print_r($rs->fields)试试,$rs->fields[&#39;字段名&#39;],返回的是这个字段里的值        
    $rs->MoveNext();//将指针指到下一条记录,否则出现死循环!        }        
$rs->Close();//关闭以便释放内存        
//插入新记录        $sql = "Insert table (user_type,username) VALUES (3, &#39;liucheng&#39;)";        
$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 &#39;没有找到此记录&#39;;        
    exit();        
} else {        
    echo $data_ary[&#39;username&#39;] . &#39; &#39; . $data_ary[&#39;password&#39;] . &#39; &#39; . $data_ary[&#39;user_type&#39;] . &#39;<br>&#39;;        
}        
//另一种方法        $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 &#39;没有找到此记录&#39;;        
    exit();        
} else {        
    echo $result[&#39;username&#39;] . &#39; &#39; . $result[&#39;password&#39;] . &#39; &#39; . $result[&#39;user_type&#39;] . &#39;<br>&#39;;   
}        
// 取单个字段        
//$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, &#39;liucheng&#39;)";     
$db->Execute($sql);        
$data_id = $db->Insert_ID();        
echo $data_id;        
/*$db->GenID($seqName = &#39;adodbseq&#39;,$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(&#39;user_id_seq&#39;);        
$sql = "Insert table (id, user_type,username) VALUES (" . $user_id . ", 3, &#39;liucheng&#39;)";        
$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[&#39;username&#39;]        
    echo $rs->fields[&#39;username&#39;] . &#39;<br>&#39;;//print_r($rs->fields)试试,$rs->fields[&#39;字段名&#39;],返回的是这个字段里的值        
    $rs->MoveNext();//将指针指到下一条记录,不用的话,会出现死循环!        }        
$username_ary = array_unique($username_ary);        
$rs->MoveFirst();//将指针指回第一条记录        while (!$rs->EOF) {        
    echo $rs->fields[&#39;password&#39;] . &#39;<br>&#39;;//print_r($rs->fields)试试,$rs->fields[&#39;字段名&#39;],返回的是这个字段里的值        
    $rs->MoveNext();//将指针指到下一条记录        }        
$rs->Close();        
//当本页程序,对数据库的操作完毕后,要$db->Close();        $db->Close();        
/*一个不错的方法 */       if (isset($db)) {        
    $db->Close();        
}        
?>
Copy after login

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

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 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

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.

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.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

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

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 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.

See all articles