-
强大的php数据库操作类,支持mysql,mssql,pg
下面我们来分享一下我珍藏的php数据库操作类,他的强大之处是可支持mysql,mssql,pg三大主流数据库,你只要修改一下数据库的类型就可以直接应用.
本文实例讲述了可用mysql,mssql,pg三种数据库的数据库操作类,你只要作任何修改就可以方便的改变你数据库的类型.分享给大家供大家参考,具体分析如下.
函数清单,索引:
- Open:打开数据库连接 Line:71
- Close:关闭数据库连接 Line:107
- SelectDB:选择数据库 Line:129
- Query:创建查询 Line:151
- DataSeek:移动记录指针 Line:175
- FieldName:获取字段名称 Line:198
- FieldType:获取字段类型 Line:220
- FieldLenght:获取字段长度 Line:242
- FetchRow:获取数据并保存到数组(数字索引) Line:264
- FetchArray:获取数据并保存进数组(数字和关联) Line:289
- FetchObject:获取数据并保存到对象(对象方式) Line:315
- Result:获取结果数据 Line:341
- FreeResult:刷新记录集 Line:363
- RowsNumber:获取记录数量 Line:385
- FieldsNumber:获取字段数量 Line:407
- CurRecNumber:获取当前记录号(从0开始) Line:429
- RecordNumber:获取当前行号(从1开始) Line:438
- MoveFirstRec:移动到第一条记录 Line:447
- MoveLastRec:移动到最后一条记录 Line:469
- MovePreviousRec:移动到前一条记录 Line:495
- MoveNextRec:移动到下一条记录 Line:521
- MoveToRec:移动到一个特定的记录(从1开始) Line:548
php数据库操作类代码如下:
- <?PHP
- /**********************************************************************************
- 此类将数据库操作封装,具有良好的可移植性,针对数据库:mysql,mssql,pg
- ************************************************************************************
- // -函数清单 索引:
- // - Open: 打开数据库连接 Line:71
- // - Close: 关闭数据库连接 Line:107
- // - SelectDB: 选择数据库 Line:129
- // - Query: 创建查询 Line:151
- // - DataSeek: 移动记录指针 Line:175
- // - FieldName: 获取字段名称 Line:198
- // - FieldType: 获取字段类型 Line:220
- // - FieldLenght: 获取字段长度 Line:242
- // - FetchRow: 获取数据并保存到数组(数字索引) Line:264
- // - FetchArray: 获取数据并保存进数组(数字和关联) Line:289
- // - FetchObject: 获取数据并保存到对象(对象方式) Line:315
- // - Result: 获取结果数据 Line:341
- // - FreeResult: 刷新记录集 Line:363
- // - RowsNumber: 获取记录数量 Line:385
- // - FieldsNumber: 获取字段数量 Line:407
- // - CurRecNumber: 获取当前记录号(从0开始) Line:429
- // - RecordNumber: 获取当前行号(从1开始) Line:438
- // - MoveFirstRec: 移动到第一条记录 Line:447
- // - MoveLastRec: 移动到最后一条记录 Line:469
- // - MovePreviousRec: 移动到前一条记录 Line:495
- // - MoveNextRec: 移动到下一条记录 Line:521
- // - MoveToRec: 移动到一个特定的记录(从1开始) Line:548
- ************************************************************************************
- //Inputs:
- // - dbType: databases type: mssql, mysql, pg
- // - connectType: connection type: c - common connection,
- // p - open persistent connection
- // - connect: for MS SQL Server - server name,
- // for MySQL - hostname [:port] [:/path/to/socket] ,
- // for PostgreSQL - host, port, tty, options,
- // dbname (without username and password)
- // - username
- // - password
- // - dbName: database name
- // - query: SQL query
- // - result: result set identifier
- // - RowNumber:
- // - offset: field identifier
- // - ResultType: a constant and can take the following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH
- // - FieldName
- //
- //Returns:
- // - result: result set identifier
- // - connect link identifier
- // - record number (starting at 0: CurrRecNumber or starting at 1: RecordNumber)
- // - number of fields in the specified result set
- // - number of rows in the specified result set
- *************************************************************************************/
- Class mDatabase
- {
- /***********************************成员变量定义***************************************/
- var $dbType; // 数据库类型: mssql, mysql, pg
- var $connectType; // 连接类型: c - common connection, p - open persistent connection
- var $idCon; // 连接号
- var $curRow; // current row number of data from the result
- // associated with the specified result identifier array
- var $seek; // current row number of data from DataSeek function array
- /***********************************成员方法实现***************************************/
- /************************************************************************************
- *连接数据库的函数
- *************************************************************************************/
- Function Open($dbType, $c, $connect, $username = "", $password = "")
- {
- $this->dbType = $dbType;
- Switch ($dbType) {
- Case "mssql":
- If ($connectType == "c") {
- $idCon = mssql_connect($connect, $username, $password);
- } Else {
- $idCon = mssql_pconnect($connect, $username, $password);
- }
- Break;
- Case "mysql":
- If ($connectType == "c") {
- $idCon = mysql_connect($connect, $username, $password);
- } Else {
- $idCon = mysql_pconnect($connect, $username, $password);
- }
- Break;
- Case "pg":
- If ($connectType == "c") {
- $idCon = pg_connect($connect . " user=" . $username . " password=" . $password);
- } Else {
- $idCon = pg_pconnect($connect . " user=" . $username . " password=" . $password);
- }
- Break;
- Default:
- $idCon = 0;
- Break;
- }
- $this->idCon = $idCon;
- Return $idCon;
- }
- /************************************************************************************
- *关闭数据库连接
- *************************************************************************************/
- Function Close()
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_close($this->idCon);
- Break;
- Case "mysql":
- $r = mysql_close($this->idCon);
- Break;
- Case "pg":
- $r = pg_close($this->idCon);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *选择数据库
- *************************************************************************************/
- Function SelectDb($dbName)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_select_db($dbName);
- Break;
- Case "mysql":
- $r = mysql_select_db($dbName);
- Break;
- Case "pg":
- $r = False;
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *创建查询
- *************************************************************************************/
- Function Query($query)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_query($query, $this->idCon);
- Break;
- Case "mysql":
- $r = mysql_query($query, $this->idCon);
- Break;
- Case "pg":
- $r = pg_exec($this->idCon, $query);
- Break;
- Default:
- $r = False;
- Break;
- }
- $this->curRow[$r] = 0;
- $this->seek[$r] = 0;
- Return $r;
- }
- /************************************************************************************
- *移动记录指针
- *************************************************************************************/
- Function DataSeek($result, $RowNumber)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_data_seek($result, $RowNumber);
- Break;
- Case "mysql":
- $r = mysql_data_seek($result, $RowNumber);
- Break;
- Case "pg":
- $r = False;
- Break;
- Default:
- $r = False;
- Break;
- }
- $this->seek[$result] = (int) $RowNumber;
- Return $r;
- }
- /************************************************************************************
- *获取字段名
- *************************************************************************************/
- Function FieldName($result, $offset)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_field_name($result, $offset);
- Break;
- Case "mysql":
- $r = mysql_field_name($result, $offset);
- Break;
- Case "pg":
- $r = pg_fieldname($result, $offset);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取字段类型
- *************************************************************************************/
- Function FieldType($result, $offset)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_field_type($result, $offset);
- Break;
- Case "mysql":
- $r = mysql_field_type($result, $offset);
- Break;
- Case "pg":
- $r = pg_fieldtype($result, $offset);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取字段长度
- *************************************************************************************/
- Function FieldLength($result, $offset)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_field_length($result, $offset);
- Break;
- Case "mysql":
- $r = mysql_field_len($result, $offset);
- Break;
- Case "pg":
- $r = pg_fieldsize($result, $offset);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取数据并保存到数组,可以用数字索引方式访问数组
- *************************************************************************************/
- Function FetchRow($result, $RowNumber = 0)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_fetch_row($result);
- Break;
- Case "mysql":
- $r = mysql_fetch_row($result);
- Break;
- Case "pg":
- $r = pg_fetch_row($result, $RowNumber);
- If ($r) {
- $this->curRow[$result] = $RowNumber;
- $this->seek[$result] = $RowNumber;
- }
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取数据并保存到数组,可以用数字索引和关联索引的方式访问
- *************************************************************************************/
- Function FetchArray($result, $RowNumber = 0, $ResultType = 2)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_fetch_array($result);
- Break;
- Case "mysql":
- $r = mysql_fetch_array($result);
- Break;
- Case "pg":
- $r = pg_fetch_array($result, $RowNumber, $ResultType);
- If ($r) {
- $this->curRow[$result] = $RowNumber;
- $this->seek[$result] = $RowNumber;
- }
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取数据并保存到对象
- *************************************************************************************/
- Function FetchObject($result, $RowNumber = 0, $ResultType = 2)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_fetch_object($result);
- Break;
- Case "mysql":
- $r = mysql_fetch_object($result);
- Break;
- Case "pg":
- $r = pg_fetch_object($result, $RowNumber, $ResultType);
- If ($r) {
- $this->curRow[$result] = $RowNumber;
- $this->seek[$result] = $RowNumber;
- }
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取结果数据
- *************************************************************************************/
- Function Result($result, $RowNumber, $FieldName)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_result($result, $RowNumber, $FieldName);
- Break;
- Case "mysql":
- $r = mysql_result($result, $RowNumber, $FieldName);
- Break;
- Case "pg":
- $r = pg_result($result, $RowNumber, $FieldName);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *释放结果数据
- *************************************************************************************/
- Function FreeResult($result)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_free_result($result);
- Break;
- Case "mysql":
- $r = mysql_free_result($result);
- Break;
- Case "pg":
- $r = pg_freeresult($result);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取记录数量
- *************************************************************************************/
- Function RowsNumber($result)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_num_rows($result);
- Break;
- Case "mysql":
- $r = mysql_num_rows($result);
- Break;
- Case "pg":
- $r = pg_numrows($result);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取字段数量
- *************************************************************************************/
- Function FieldsNumber($result)
- {
- Switch ($this->dbType) {
- Case "mssql":
- $r = mssql_num_fields($result);
- Break;
- Case "mysql":
- $r = mysql_num_fields($result);
- Break;
- Case "pg":
- $r = pg_numfields($result);
- Break;
- Default:
- $r = False;
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *获取当前记录号(从0开始)
- *************************************************************************************/
- Function CurRecNumber($result)
- { //开源软件:phpfensi.com
- $r = $this->curRow[$result];
- Return $r;
- }
- /************************************************************************************
- *获取当前行号(从1开始)
- *************************************************************************************/
- Function RecordNumber($result)
- {
- $cr = $this->CurRecNumber($result) + 1;
- Return $cr;
- }
- /************************************************************************************
- *移动到第一条记录
- *************************************************************************************/
- Function MoveFirstRec($result)
- {
- Switch ($this->dbType) {
- Case "pg":
- $r = $this->FetchRow($result, 0);
- Break;
- Default:
- $rn = $this->DataSeek($result, 0);
- If ($rn) {
- $r = $this->FetchRow($result);
- If ($r) $this->curRow[$result] = $this->seek[$result];
- } Else {
- $r = False;
- }
- Break;
- }
- Return $r;
- }
- /************************************************************************************
- *移动到最后一条记录
- *************************************************************************************/
- Function MoveLastRec($result)
- {
- $rs = $this->RowsNumber($result);
- If ($rs) {
- $rs--;
- Switch ($this->dbType) {
- Case "pg":
- $r = $this->FetchRow($result, $rs);
- Break;
- Default:
- $rn = $this->DataSeek($result, $rs);
- If ($rn) {
- $r = $this->FetchRow($result);
- If ($r) $this->curRow[$result] = $this->seek[$result];
- } Else {
- $r = False;
- }
- Break;
- }
- }
- Return $r;
- }
- /************************************************************************************
- *移动到前一条记录
- *************************************************************************************/
- Function MovePreviousRec($result)
- {
- $rs = $this->CurRecNumber($result);
- If ($rs) {
- $rs--;
- Switch ($this->dbType) {
- Case "pg":
- $r = $this->FetchRow($result, $rs);
- Break;
- Default:
- $rn = $this->DataSeek($result, $rs);
- If ($rn) {
- $r = $this->FetchRow($result);
- If ($r) $this->curRow[$result] = $this->seek[$result];
- } Else {
- $r = False;
- }
- Break;
- }
- }
- Return $r;
- }
- /************************************************************************************
- *移动到下一条记录
- *************************************************************************************/
- Function MoveNextRec($result)
- {
- $rs = $this->CurRecNumber($result);
- $rn = $this->RowsNumber($result);
- $rs++;
- If ($rs != $rn) {
- Switch ($this->dbType) {
- Case "pg":
- $r = $this->FetchRow($result, $rs);
- Break;
- Default:
- $re = $this->FetchRow($result);
- If ($re) {
- $r = $re;
- $this->curRow[$result]++;
- $this->seek[$result] = $this->curRow[$result];
- } Else {
- $r = False;
- }
- Break;
- }
- }
- Return $r;
- }
- /************************************************************************************
- *移动到指定记录(编号从0开始)
- *************************************************************************************/
- Function MoveToRec($result, $RowNumber)
- {
- $rn = $this->RowsNumber($result);
- If ($RowNumber > 0 And $RowNumber < $rn) {
- $RowNumber--;
- Switch ($this->dbType) {
- Case "pg":
- $r = $this->FetchRow($result, $RowNumber);
- Break;
- Default:
- $rn = $this->DataSeek($result, $RowNumber);
- If ($rn) {
- $r = $this->FetchRow($result);
- If ($r) $this->curRow[$result] = $this->seek[$result];
- } Else {
- $r = False;
- }
- Break;
- }
- }
- Return $r;
- }
- }
- //********************************方法实现完毕****************************************//
- ?>
我没有骗你吧,真的是我见过最强大的php数据库操作类.
出处:http://www.phpfensi.com/php/20150415/9313.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式