本文实例讲述了可用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数据库操作类代码如下:
复制代码 代码如下:<"", $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)
{
$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数据库程序设计有所帮助。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]