PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/csci242/Database.class.php

http://csci6442-crm.googlecode.com/
PHP | 262 lines | 165 code | 33 blank | 64 comment | 26 complexity | 9fd5a7016b9f2ddd5aed0539565f2c9b MD5 | raw file
  1. <?php
  2. /**
  3. *Database module
  4. *@author Yaxing Chen
  5. *@team SET
  6. *@date 03/26/2011
  7. */
  8. class Database
  9. {
  10. private $pConnect=FALSE;//permernent connect permission
  11. private $mHost;//host name
  12. private $db; //database name
  13. private $mUser;//user name
  14. private $mPwd;//pass word
  15. private $mConn;//connection handle
  16. private $result;//results set
  17. private $num_rows;// number of result rows
  18. private $insert_id;// the id of the last insert instruction
  19. private $affected_rows;// affected rows number by query instructions
  20. // INSERT UPDATE or DELETE
  21. /*
  22. * Construction function
  23. *
  24. * @param: if no arguments - connect database as configured user
  25. * or connect database as defined user
  26. */
  27. public function Database($user='', $pwd=''){
  28. $database = require('Dbconfig.php');
  29. $this->db = $database['dbName'];
  30. $this->mHost = $database['host'];
  31. $this->mUser = $database['user'];
  32. $this->mPwd = $database['pwd'];
  33. if(strlen($user) > 0 && strlen($pwd) > 0){
  34. $this->mUser = $user;
  35. $this->mPwd = $pwd;
  36. }
  37. }
  38. /*
  39. * connect mysql
  40. */
  41. public function connect(){
  42. if($this->pConnect){
  43. $this->mConn=mysql_pconnect($this->mHost,$this->mUser,$this->mPwd);
  44. }
  45. else{
  46. $this->mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);//short connect
  47. }
  48. if(!$this->mConn){
  49. $this->dbhalt();
  50. }
  51. if($this->db==""){
  52. $this->dbhalt("No database is selected!");
  53. }
  54. if(!mysql_select_db($this->db,$this->mConn)){
  55. //echo mysql_error();
  56. $this->dbhalt("Wrong database!");
  57. }
  58. $this->execute("set names UTF8");
  59. }
  60. /*
  61. * close database connection
  62. */
  63. public function dbclose(){
  64. mysql_close($this->mConn);
  65. }
  66. /*
  67. * change database
  68. */
  69. public function dbChange($newDb){
  70. $this->db=$newDb;
  71. $this->connect();
  72. }
  73. /*
  74. * execute sql and return source id
  75. */
  76. public function execute($sql){
  77. try{
  78. $this->result=mysql_query($sql);
  79. }catch(Exception $e){
  80. $this->dbhalt();
  81. }
  82. return;
  83. }
  84. /*
  85. * check sql format
  86. */
  87. private function chkSql($sql){
  88. if(strrpos($sql, ";", 0) == strlen($sql) - 1){
  89. return true;
  90. }
  91. else{
  92. return false;
  93. }
  94. }
  95. /*
  96. * define database error message
  97. */
  98. private function dbhalt($errmsg = ''){
  99. $msg=mysql_error();
  100. if($errmsg != null){
  101. if($errmsg != ''){
  102. $msg=$errmsg;
  103. }
  104. }
  105. echo $msg;
  106. die();
  107. }
  108. /*
  109. * get result as an indexed and associated array
  110. */
  111. public function fetchArray($resultType=MYSQL_BOTH){
  112. $all = array();
  113. while (($tmp[] = mysql_fetch_array($this->result)) == true) {$all = $tmp;}
  114. return $all;
  115. }
  116. /*
  117. * get result as an associate array
  118. */
  119. public function fetchAssoc(){
  120. $all = array();
  121. while (($tmp[] = mysql_fetch_assoc($this->result)) == true) {$all = $tmp;}
  122. return $all;
  123. }
  124. /*
  125. * get result as an index array
  126. */
  127. public function fetchIndexArray(){
  128. $all = array();
  129. while (($tmp[] = mysql_fetch_row($this->result)) == true) {$all = $tmp;}
  130. return $all;
  131. }
  132. /*
  133. * get result as an object array
  134. */
  135. public function fetchObject(){
  136. $all = array();
  137. while (($tmp[] = mysql_fetch_object($this->result)) == true) {$all = $tmp;}
  138. return $all;
  139. }
  140. /*
  141. * return number of result rows
  142. */
  143. public function numRows(){
  144. if($this->result == null){
  145. return 0;
  146. }
  147. return mysql_num_rows($this->result);
  148. }
  149. /*
  150. * return all database name in local host
  151. */
  152. public function dbNames(){
  153. $rsPtr=mysql_list_dbs($this->mConn);
  154. $i=0;
  155. $cnt=mysql_num_rows($rsPtr);
  156. while($i<$cnt)
  157. {
  158. $rs[]=mysql_db_name($rsPtr,$i);
  159. $i++;
  160. }
  161. return $rs;
  162. }
  163. /*
  164. * select
  165. */
  166. public function select($sql){
  167. if(!$this->chkSql($sql)){
  168. $sql = $sql.";";
  169. }
  170. $this->connect();
  171. $this->execute($sql);
  172. $this->dbclose();
  173. return $this->result;
  174. }
  175. /*
  176. * delete
  177. */
  178. public function delete($sql){
  179. if(!$this->chkSql($sql)){
  180. $sql = $sql.";";
  181. }
  182. try{
  183. $this->connect();
  184. $this->result=$this->execute($sql);
  185. $this->affected_rows=mysql_affected_rows($this->mConn);
  186. //$this->free_result($result);
  187. $this->dbclose();
  188. return $this->affected_rows;
  189. }catch(Exception $e){
  190. $this->dbhalt();
  191. }
  192. }
  193. /*
  194. * insert
  195. */
  196. public function insert($sql){
  197. if(!$this->chkSql($sql)){
  198. $sql = $sql.";";
  199. }
  200. try{
  201. $this->connect();
  202. $this->result=$this->execute($sql);
  203. $this->insert_id=mysql_insert_id($this->mConn);
  204. $this->dbclose();
  205. return $this->insert_id;
  206. }catch(Exception $e){
  207. $this->dbhalt();
  208. }
  209. }
  210. /*
  211. * update
  212. */
  213. public function update($sql){
  214. if(!$this->chkSql($sql)){
  215. $sql = $sql.";";
  216. }
  217. try{
  218. $this->connect();
  219. $this->result=$this->execute($sql);
  220. $this->affected_rows=mysql_affected_rows($this->mConn);
  221. //$this->free_result($result);
  222. $this->dbclose();
  223. return $this->affected_rows;
  224. }catch(Exception $e){
  225. $this->dbhalt();
  226. }
  227. }
  228. }// end class
  229. ?>