强大的php数据库操作类,支持mysql,mssql,pg

下面我们来分享一下我珍藏的php数据库操作类,他的强大之处是可支持mysql,mssql,pg三大主流数据库,你只要修改一下数据库的类型就可以直接应用.

本文实例讲述了可用mysql,mssql,pg三种数据库的数据库操作类,你只要作任何修改就可以方便的改变你数据库的类型.分享给大家供大家参考,具体分析如下.

函数清单,索引:

  1. Open:打开数据库连接 Line:71
  2. Close:关闭数据库连接 Line:107
  3. SelectDB:选择数据库 Line:129
  4. Query:创建查询 Line:151
  5. DataSeek:移动记录指针 Line:175
  6. FieldName:获取字段名称 Line:198
  7. FieldType:获取字段类型 Line:220
  8. FieldLenght:获取字段长度 Line:242
  9. FetchRow:获取数据并保存到数组(数字索引) Line:264
  10. FetchArray:获取数据并保存进数组(数字和关联) Line:289
  11. FetchObject:获取数据并保存到对象(对象方式) Line:315
  12. Result:获取结果数据 Line:341
  13. FreeResult:刷新记录集 Line:363
  14. RowsNumber:获取记录数量 Line:385
  15. FieldsNumber:获取字段数量 Line:407
  16. CurRecNumber:获取当前记录号(从0开始) Line:429
  17. RecordNumber:获取当前行号(从1开始) Line:438
  18. MoveFirstRec:移动到第一条记录 Line:447
  19. MoveLastRec:移动到最后一条记录 Line:469
  20. MovePreviousRec:移动到前一条记录 Line:495
  21. MoveNextRec:移动到下一条记录 Line:521
  22. MoveToRec:移动到一个特定的记录(从1开始) Line:548

php数据库操作类代码如下:

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

我没有骗你吧,真的是我见过最强大的php数据库操作类.