Home php教程 php手册 PHP ADOdb使用手册:ADOdb安装与Mysql连接教程

PHP ADOdb使用手册:ADOdb安装与Mysql连接教程

Jun 21, 2016 am 08:53 AM
mysql php

  在PHP网站开发中,针对数据库平台移植性的解决方案,之前介绍过PEAR DB类安装使用的PHP教程,其实使用PHP ADOdb类库也是一种很好的解决方案,其支持的数据库引擎更多。

  作为PHP ADOdb使用手册开篇文章,主要介绍使用PHP ADOdb类库具有哪些优点,PHP ADOdb类库的基本安装使用方法,以及使用PHP ADOdb建立Mysql数据库连接的几种方法。

使用PHP ADOdb的优点

  1、与PHP结合好,速度快

  2、扩展性好,对于windows平台的程序员来说易于掌握,因为和Microsoft’s ADO的类库比较类似

  3、使用起来容易上手

  4、每个版本都是在Access, MySQL, PostgreSQL, MS SQL, Oracle 11g上独立测试,质量保证,支持多种数据库。

  5、PHP4版本支持使用PHP ADOdb类库来存储session变量,便于扩展和移植

PHP ADOdb类库的相关文件说明

  Adodb.inc.php为主要文件,使用adodb类时,只要include这个文件即可

  Adodb-*.inc.php是特定的数据库驱动程序代码

  Test.php包含测试adodb类库的测试数据库列表,在tests目录下

  Adodb-session.php是PHP4中的session 处理代码

  Testdatabases.inc.php包含应用于测试的数据库列表,被Test.php include

  tute.htm是PHP ADOdbadodb类英文版使用教程。

PHP ADOdb类库的基本安装使用方法

  1、首先需要下载PHP ADOdb类库,当前ADOdb类库版本为ADOdb5

  adodb类库下载地址

  2、开始PHP ADOdb类库安装

  PHP ADOdb类库安装主要有两种方式,一种是包含完整的源代码,另一种最小化安装方式。

  首先确保运行的PHP版本在4.0.5以上,然后解压缩PHP ADOdb类库文件至web服务器的相关目录,PHP ADOdb类库安装就完成了。查看PHP运行环境配置教程

  PHP ADOdb类库最小化安装方式

  在最小化安装PHP ADOdb类库时必须包含以下文件

  adodb.inc.php

  adodb-lib.inc.php

  adodb-time.inc.php

  drivers/adodb-$database.inc.php

  license.txt

  adodb-php4.inc.php

  adodb-iterator.inc.php

  安装可选项:

  adodb-error.inc.php and lang/adodb-$lang.inc.php (可使用MetaError())

  adodb-csvlib.inc.php (如果你打算使用缓存记录,需要用到CacheExecute()等)

  adodb-exceptions.inc.php 和 adodb-errorhandler.inc.php (如果你使用adodb处理错误或者PHP5中的错误异常).

  adodb-active-record.inc.php (Active Records 号称可以将数据库中的表和记录与本地的PHP对象独立开来,让程序员将更多的精力集中在数据处理上,而不是SQL语句,MVC的味道,呵呵)。

  在完成PHP ADOdb类库的安装工作后,我们开始PHP ADOdb类库的使用之旅。

PHP ADOdb使用之Mysql数据库连接建立方法

  PHP ADOdb类库支持多种类型的数据库,本教程首先介绍最基本的使用PHP ADOdb类库与Mysql数据库建立连接(connect)的方法,与PEAR DB类库类似,PHP ADOdb类库建立Mysql数据库连接(connect)也有两种方法,一种使用ADONewConnection和Connect函数建立连接,一种使用DSN建立连接。如何使用PEAR DB类?

使用PHP ADOdb建立Mysql连接方法一

1
2
3
4
5
6
7
8
9
10
11


  include('adodb5/adodb.inc.php');
  $dbdriver = 'mysql';
  $db = ADONewConnection($dbdriver);
  $db->debug = true;//调试,显示具体的SQL语句
  $db->Connect('localhost', 'root', '123456', 'test');
  $rs = $db->Execute('select * from leapsoul');
  print "

"
Copy after login
Copy after login
;
  print_r($rs->GetRows());
  print "";
?>

注释

1、在使用PHP ADOdb类库建立Mysql数据库连接时,你需要使用PHP ADOdb类库的ADONewConnection($driver)函数创建一个连接,你可以选择建立哪种数据库,比如Access等,这里我使用Mysql数据库建立连接。

2、然后你需要决定是使用永久性连接还是非永久连接,永久连接速度更快,数据库连接不会关闭除非你使用close()函数;非永久连接尽快占用的资源少,但是风险在于数据库和web服务器的负荷会比较重。你可以根据项目的具体需求自行决定。

3、然后通过Execute函数执行SQL语句,再以数组结构显示查询leapsoul表中关于www.leapsoul.cn的相关信息。

知识点

1、NewADOConnection($driver)是ADONewConnection($driver)的别名,功能一样,建立连接时,ADONewConnection($dbdriver)与&ADONewConnection($dbdriver)没啥区别。

2、永久连接使用$conn->PConnect(),非永久使用$conn->Connect(),有些数据库也支持NConnect(),此函数会强制创建新的数据库连接

3、如果你同时创建了永久和非永久两种连接,并使用相同的userid和password,PHP将会共享同一个连接,当它们连接不同数据库时,会出现问题,解决方法是对不同的数据库总是使用不同的userid或者使用NConnect()

使用PHP ADOdb建立Mysql连接方法二

PHP ADOdb类库从4.51版本开始支持以DSN方式连接数据库

DSN格式如下

1

$driver://$username:$password@hostname/$database?options[=value]

  options主要有以下选项

  所有数据库都支持的选项:’persist’, ‘persistent’, ‘debug’, ‘fetchmode’, ‘new’ , ‘cachesecs’, ‘memcache’
  Interbase/Firebird:’dialect’,'charset’,'buffers’,'role’
  M’soft ADO:’charpage’
  MySQL:’clientflags’
  MySQLi:’port’, ’socket’, ‘clientflags’
  Oci8:’nls_date_format’,'charset’

  上述选项如果options的value没有赋值,则默认值为1

  cachesecs决定了当CacheExecute()和CacheSelectLimit()函数被调用,同时这两个函数又没有设定cache-time参数值时,recordsets缓存多少秒,默认缓存3600秒

  memcache定义了memcache的主机地址,端口以及是否使用压缩机制。

  cachesecs和memcache两个参数是从PHP ADOdb 5.09版本开始增加的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


  include('adodb5/adodb.inc.php');

  #非永久连接方式
  $dsn = 'mysql://root:123456@localhost/test';
  $db = NewADOConnection($dsn);
  if (!$db) die("Connection failed");

  # 永久连接
  //$dsn2 = 'mysql://root:pwd@localhost/mydb?persist';

  # 非永久连接,数据库端口为3000
  // $dsn2 = 'mysqli://root:pwd@localhost/mydb?persist=0&port=3000';

  $db->debug = true;

  $rs = $db->Execute('select * from leapsoul');

  print "

"
Copy after login
Copy after login
;
  print_r($rs->GetRows());
  print "";
?>

知识点

  使用DSN方式连接数据库时NewADOConnection()内部调用Connect()或者PConnect()函数,如果连接失败,则返回False

  至此,在完成PHP ADOdb类库安装后,使用PHP ADOdb类库建立Mysql数据库连接的方法就介绍完了,大部分数据库建立连接的方法与Mysql数据库建立连接方法是一样的,下次将会介绍PHP ADOdb使用手册之Access等数据库的连接方法。

  :PHP网站开发教程-leapsoul.cn版权所有,转载时请以链接形式注明原始出处及本声明,谢谢。



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

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 fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

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

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

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

Top 10 PHP CMS Platforms For Developers in 2024 Top 10 PHP CMS Platforms For Developers in 2024 Dec 05, 2024 am 10:29 AM

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? The page is blank after PHP is connected to MySQL. What is the reason for the invalid die() function? Apr 01, 2025 pm 03:03 PM

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

See all articles