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数据库操作类代码如下:

  1. <?PHP
  2. /**********************************************************************************
  3. 此类将数据库操作封装,具有良好的可移植性,针对数据库:mysql,mssql,pg
  4. *原作者:Andrzej Manczyk 整理:玉面修罗 E-mail/msn:xiuluo-999@163.com 时间:2006-12-20*
  5. ************************************************************************************
  6. // -函数清单 索引:
  7. // - Open: 打开数据库连接 Line:71
  8. // - Close: 关闭数据库连接 Line:107
  9. // - SelectDB: 选择数据库 Line:129
  10. // - Query: 创建查询 Line:151
  11. // - DataSeek: 移动记录指针 Line:175
  12. // - FieldName: 获取字段名称 Line:198
  13. // - FieldType: 获取字段类型 Line:220
  14. // - FieldLenght: 获取字段长度 Line:242
  15. // - FetchRow: 获取数据并保存到数组(数字索引) Line:264
  16. // - FetchArray: 获取数据并保存进数组(数字和关联) Line:289
  17. // - FetchObject: 获取数据并保存到对象(对象方式) Line:315
  18. // - Result: 获取结果数据 Line:341
  19. // - FreeResult: 刷新记录集 Line:363
  20. // - RowsNumber: 获取记录数量 Line:385
  21. // - FieldsNumber: 获取字段数量 Line:407
  22. // - CurRecNumber: 获取当前记录号(从0开始) Line:429
  23. // - RecordNumber: 获取当前行号(从1开始) Line:438
  24. // - MoveFirstRec: 移动到第一条记录 Line:447
  25. // - MoveLastRec: 移动到最后一条记录 Line:469
  26. // - MovePreviousRec: 移动到前一条记录 Line:495
  27. // - MoveNextRec: 移动到下一条记录 Line:521
  28. // - MoveToRec: 移动到一个特定的记录(从1开始) Line:548
  29. ************************************************************************************
  30. //Inputs:
  31. // - dbType: databases type: mssql, mysql, pg
  32. // - connectType: connection type: c - common connection,
  33. // p - open persistent connection
  34. // - connect: for MS SQL Server - server name,
  35. // for MySQL - hostname [:port] [:/path/to/socket] ,
  36. // for PostgreSQL - host, port, tty, options,
  37. // dbname (without username and password)
  38. // - username
  39. // - password
  40. // - dbName: database name
  41. // - query: SQL query
  42. // - result: result set identifier
  43. // - RowNumber:
  44. // - offset: field identifier
  45. // - ResultType: a constant and can take the following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH
  46. // - FieldName
  47. //
  48. //Returns:
  49. // - result: result set identifier
  50. // - connect link identifier
  51. // - record number (starting at 0: CurrRecNumber or starting at 1: RecordNumber)
  52. // - number of fields in the specified result set
  53. // - number of rows in the specified result set
  54. *************************************************************************************/
  55. Class mDatabase
  56. {
  57. /***********************************成员变量定义***************************************/
  58. var $dbType; // 数据库类型: mssql, mysql, pg
  59. var $connectType; // 连接类型: c - common connection, p - open persistent connection
  60. var $idCon; // 连接号
  61. var $curRow; // current row number of data from the result
  62. // associated with the specified result identifier array
  63. var $seek; // current row number of data from DataSeek function array
  64. /***********************************成员方法实现***************************************/
  65. /************************************************************************************
  66. *连接数据库的函数
  67. *************************************************************************************/
  68. Function Open($dbType, $c, $connect, $username = "", $password = "")
  69. {
  70. $this->dbType = $dbType;
  71. Switch ($dbType) {
  72. Case "mssql":
  73. If ($connectType == "c") {
  74. $idCon = mssql_connect($connect, $username, $password);
  75. } Else {
  76. $idCon = mssql_pconnect($connect, $username, $password);
  77. }
  78. Break;
  79. Case "mysql":
  80. If ($connectType == "c") {
  81. $idCon = mysql_connect($connect, $username, $password);
  82. } Else {
  83. $idCon = mysql_pconnect($connect, $username, $password);
  84. }
  85. Break;
  86. Case "pg":
  87. If ($connectType == "c") {
  88. $idCon = pg_connect($connect . " user=" . $username . " password=" . $password);
  89. } Else {
  90. $idCon = pg_pconnect($connect . " user=" . $username . " password=" . $password);
  91. }
  92. Break;
  93. Default:
  94. $idCon = 0;
  95. Break;
  96. }
  97. $this->idCon = $idCon;
  98. Return $idCon;
  99. }
  100. /************************************************************************************
  101. *关闭数据库连接
  102. *************************************************************************************/
  103. Function Close()
  104. {
  105. Switch ($this->dbType) {
  106. Case "mssql":
  107. $r = mssql_close($this->idCon);
  108. Break;
  109. Case "mysql":
  110. $r = mysql_close($this->idCon);
  111. Break;
  112. Case "pg":
  113. $r = pg_close($this->idCon);
  114. Break;
  115. Default:
  116. $r = False;
  117. Break;
  118. }
  119. Return $r;
  120. }
  121. /************************************************************************************
  122. *选择数据库
  123. *************************************************************************************/
  124. Function SelectDb($dbName)
  125. {
  126. Switch ($this->dbType) {
  127. Case "mssql":
  128. $r = mssql_select_db($dbName);
  129. Break;
  130. Case "mysql":
  131. $r = mysql_select_db($dbName);
  132. Break;
  133. Case "pg":
  134. $r = False;
  135. Break;
  136. Default:
  137. $r = False;
  138. Break;
  139. }
  140. Return $r;
  141. }
  142. /************************************************************************************
  143. *创建查询
  144. *************************************************************************************/
  145. Function Query($query)
  146. {
  147. Switch ($this->dbType) {
  148. Case "mssql":
  149. $r = mssql_query($query, $this->idCon);
  150. Break;
  151. Case "mysql":
  152. $r = mysql_query($query, $this->idCon);
  153. Break;
  154. Case "pg":
  155. $r = pg_exec($this->idCon, $query);
  156. Break;
  157. Default:
  158. $r = False;
  159. Break;
  160. }
  161. $this->curRow[$r] = 0;
  162. $this->seek[$r] = 0;
  163. Return $r;
  164. }
  165. /************************************************************************************
  166. *移动记录指针
  167. *************************************************************************************/
  168. Function DataSeek($result, $RowNumber)
  169. {
  170. Switch ($this->dbType) {
  171. Case "mssql":
  172. $r = mssql_data_seek($result, $RowNumber);
  173. Break;
  174. Case "mysql":
  175. $r = mysql_data_seek($result, $RowNumber);
  176. Break;
  177. Case "pg":
  178. $r = False;
  179. Break;
  180. Default:
  181. $r = False;
  182. Break;
  183. }
  184. $this->seek[$result] = (int) $RowNumber;
  185. Return $r;
  186. }
  187. /************************************************************************************
  188. *获取字段名
  189. *************************************************************************************/
  190. Function FieldName($result, $offset)
  191. {
  192. Switch ($this->dbType) {
  193. Case "mssql":
  194. $r = mssql_field_name($result, $offset);
  195. Break;
  196. Case "mysql":
  197. $r = mysql_field_name($result, $offset);
  198. Break;
  199. Case "pg":
  200. $r = pg_fieldname($result, $offset);
  201. Break;
  202. Default:
  203. $r = False;
  204. Break;
  205. }
  206. Return $r;
  207. }
  208. /************************************************************************************
  209. *获取字段类型
  210. *************************************************************************************/
  211. Function FieldType($result, $offset)
  212. {
  213. Switch ($this->dbType) {
  214. Case "mssql":
  215. $r = mssql_field_type($result, $offset);
  216. Break;
  217. Case "mysql":
  218. $r = mysql_field_type($result, $offset);
  219. Break;
  220. Case "pg":
  221. $r = pg_fieldtype($result, $offset);
  222. Break;
  223. Default:
  224. $r = False;
  225. Break;
  226. }
  227. Return $r;
  228. }
  229. /************************************************************************************
  230. *获取字段长度
  231. *************************************************************************************/
  232. Function FieldLength($result, $offset)
  233. {
  234. Switch ($this->dbType) {
  235. Case "mssql":
  236. $r = mssql_field_length($result, $offset);
  237. Break;
  238. Case "mysql":
  239. $r = mysql_field_len($result, $offset);
  240. Break;
  241. Case "pg":
  242. $r = pg_fieldsize($result, $offset);
  243. Break;
  244. Default:
  245. $r = False;
  246. Break;
  247. }
  248. Return $r;
  249. }
  250. /************************************************************************************
  251. *获取数据并保存到数组,可以用数字索引方式访问数组
  252. *************************************************************************************/
  253. Function FetchRow($result, $RowNumber = 0)
  254. {
  255. Switch ($this->dbType) {
  256. Case "mssql":
  257. $r = mssql_fetch_row($result);
  258. Break;
  259. Case "mysql":
  260. $r = mysql_fetch_row($result);
  261. Break;
  262. Case "pg":
  263. $r = pg_fetch_row($result, $RowNumber);
  264. If ($r) {
  265. $this->curRow[$result] = $RowNumber;
  266. $this->seek[$result] = $RowNumber;
  267. }
  268. Break;
  269. Default:
  270. $r = False;
  271. Break;
  272. }
  273. Return $r;
  274. }
  275. /************************************************************************************
  276. *获取数据并保存到数组,可以用数字索引和关联索引的方式访问
  277. *************************************************************************************/
  278. Function FetchArray($result, $RowNumber = 0, $ResultType = 2)
  279. {
  280. Switch ($this->dbType) {
  281. Case "mssql":
  282. $r = mssql_fetch_array($result);
  283. Break;
  284. Case "mysql":
  285. $r = mysql_fetch_array($result);
  286. Break;
  287. Case "pg":
  288. $r = pg_fetch_array($result, $RowNumber, $ResultType);
  289. If ($r) {
  290. $this->curRow[$result] = $RowNumber;
  291. $this->seek[$result] = $RowNumber;
  292. }
  293. Break;
  294. Default:
  295. $r = False;
  296. Break;
  297. }
  298. Return $r;
  299. }
  300. /************************************************************************************
  301. *获取数据并保存到对象
  302. *************************************************************************************/
  303. Function FetchObject($result, $RowNumber = 0, $ResultType = 2)
  304. {
  305. Switch ($this->dbType) {
  306. Case "mssql":
  307. $r = mssql_fetch_object($result);
  308. Break;
  309. Case "mysql":
  310. $r = mysql_fetch_object($result);
  311. Break;
  312. Case "pg":
  313. $r = pg_fetch_object($result, $RowNumber, $ResultType);
  314. If ($r) {
  315. $this->curRow[$result] = $RowNumber;
  316. $this->seek[$result] = $RowNumber;
  317. }
  318. Break;
  319. Default:
  320. $r = False;
  321. Break;
  322. }
  323. Return $r;
  324. }
  325. /************************************************************************************
  326. *获取结果数据
  327. *************************************************************************************/
  328. Function Result($result, $RowNumber, $FieldName)
  329. {
  330. Switch ($this->dbType) {
  331. Case "mssql":
  332. $r = mssql_result($result, $RowNumber, $FieldName);
  333. Break;
  334. Case "mysql":
  335. $r = mysql_result($result, $RowNumber, $FieldName);
  336. Break;
  337. Case "pg":
  338. $r = pg_result($result, $RowNumber, $FieldName);
  339. Break;
  340. Default:
  341. $r = False;
  342. Break;
  343. }
  344. Return $r;
  345. }
  346. /************************************************************************************
  347. *释放结果数据
  348. *************************************************************************************/
  349. Function FreeResult($result)
  350. {
  351. Switch ($this->dbType) {
  352. Case "mssql":
  353. $r = mssql_free_result($result);
  354. Break;
  355. Case "mysql":
  356. $r = mysql_free_result($result);
  357. Break;
  358. Case "pg":
  359. $r = pg_freeresult($result);
  360. Break;
  361. Default:
  362. $r = False;
  363. Break;
  364. }
  365. Return $r;
  366. }
  367. /************************************************************************************
  368. *获取记录数量
  369. *************************************************************************************/
  370. Function RowsNumber($result)
  371. {
  372. Switch ($this->dbType) {
  373. Case "mssql":
  374. $r = mssql_num_rows($result);
  375. Break;
  376. Case "mysql":
  377. $r = mysql_num_rows($result);
  378. Break;
  379. Case "pg":
  380. $r = pg_numrows($result);
  381. Break;
  382. Default:
  383. $r = False;
  384. Break;
  385. }
  386. Return $r;
  387. }
  388. /************************************************************************************
  389. *获取字段数量
  390. *************************************************************************************/
  391. Function FieldsNumber($result)
  392. {
  393. Switch ($this->dbType) {
  394. Case "mssql":
  395. $r = mssql_num_fields($result);
  396. Break;
  397. Case "mysql":
  398. $r = mysql_num_fields($result);
  399. Break;
  400. Case "pg":
  401. $r = pg_numfields($result);
  402. Break;
  403. Default:
  404. $r = False;
  405. Break;
  406. }
  407. Return $r;
  408. }
  409. /************************************************************************************
  410. *获取当前记录号(从0开始)
  411. *************************************************************************************/
  412. Function CurRecNumber($result)
  413. {
  414. $r = $this->curRow[$result];
  415. Return $r;
  416. }
  417. /************************************************************************************
  418. *获取当前行号(从1开始)
  419. *************************************************************************************/
  420. Function RecordNumber($result)
  421. {
  422. $cr = $this->CurRecNumber($result) + 1;
  423. Return $cr;
  424. }
  425. /************************************************************************************
  426. *移动到第一条记录
  427. *************************************************************************************/
  428. Function MoveFirstRec($result)
  429. {
  430. Switch ($this->dbType) {
  431. Case "pg":
  432. $r = $this->FetchRow($result, 0);
  433. Break;
  434. Default:
  435. $rn = $this->DataSeek($result, 0);
  436. If ($rn) {
  437. $r = $this->FetchRow($result);
  438. If ($r) $this->curRow[$result] = $this->seek[$result];
  439. } Else {
  440. $r = False;
  441. }
  442. Break;
  443. }
  444. Return $r;
  445. }
  446. /************************************************************************************
  447. *移动到最后一条记录
  448. *************************************************************************************/
  449. Function MoveLastRec($result)
  450. {
  451. $rs = $this->RowsNumber($result);
  452. If ($rs) {
  453. $rs--;
  454. Switch ($this->dbType) {
  455. Case "pg":
  456. $r = $this->FetchRow($result, $rs);
  457. Break;
  458. Default:
  459. $rn = $this->DataSeek($result, $rs);
  460. If ($rn) {
  461. $r = $this->FetchRow($result);
  462. If ($r) $this->curRow[$result] = $this->seek[$result];
  463. } Else {
  464. $r = False;
  465. }
  466. Break;
  467. }
  468. }
  469. Return $r;
  470. }
  471. /************************************************************************************
  472. *移动到前一条记录
  473. *************************************************************************************/
  474. Function MovePreviousRec($result)
  475. {
  476. $rs = $this->CurRecNumber($result);
  477. If ($rs) {
  478. $rs--;
  479. Switch ($this->dbType) {
  480. Case "pg":
  481. $r = $this->FetchRow($result, $rs);
  482. Break;
  483. Default:
  484. $rn = $this->DataSeek($result, $rs);
  485. If ($rn) {
  486. $r = $this->FetchRow($result);
  487. If ($r) $this->curRow[$result] = $this->seek[$result];
  488. } Else {
  489. $r = False;
  490. }
  491. Break;
  492. }
  493. }
  494. Return $r;
  495. }
  496. /************************************************************************************
  497. *移动到下一条记录
  498. *************************************************************************************/
  499. Function MoveNextRec($result)
  500. {
  501. $rs = $this->CurRecNumber($result);
  502. $rn = $this->RowsNumber($result);
  503. $rs++;
  504. If ($rs != $rn) {
  505. Switch ($this->dbType) {
  506. Case "pg":
  507. $r = $this->FetchRow($result, $rs);
  508. Break;
  509. Default:
  510. $re = $this->FetchRow($result);
  511. If ($re) {
  512. $r = $re;
  513. $this->curRow[$result]++;
  514. $this->seek[$result] = $this->curRow[$result];
  515. } Else {
  516. $r = False;
  517. }
  518. Break;
  519. }
  520. }
  521. Return $r;
  522. }
  523. /************************************************************************************
  524. *移动到指定记录(编号从0开始)
  525. *************************************************************************************/
  526. Function MoveToRec($result, $RowNumber)
  527. {
  528. $rn = $this->RowsNumber($result);
  529. If ($RowNumber > 0 And $RowNumber < $rn) {
  530. $RowNumber--;
  531. Switch ($this->dbType) {
  532. Case "pg":
  533. $r = $this->FetchRow($result, $RowNumber);
  534. Break;
  535. Default:
  536. $rn = $this->DataSeek($result, $RowNumber);
  537. //开源代码phpfensi.com
  538. If ($rn) {
  539. $r = $this->FetchRow($result);
  540. If ($r) $this->curRow[$result] = $this->seek[$result];
  541. } Else {
  542. $r = False;
  543. }
  544. Break;
  545. }
  546. }
  547. Return $r;
  548. }
  549. }
  550. //********************************方法实现完毕****************************************//
  551. ?>