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

Video Face Swap

Video Face Swap

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

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

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles