PHP MySQL Data Connection Class


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. Class DataConnection {
  3.  
  4. var $oConn;
  5. var $oDB;
  6. var $oCurrentResource;
  7. var $aResultSet;
  8. var $bResultsFound;
  9.  
  10.  
  11. function DataConnection()
  12. {
  13. $this->oConn = mysql_connect(DB_SERVER, DB_USER, DB_USER_PASS) or die("No connection!");
  14. $this->oDB = mysql_select_db(DB_NAME) or die("No database!");
  15. register_shutdown_function(array(&$this, "closeConnection"));
  16.  
  17. $this->bResultsFound = FALSE;
  18. }
  19.  
  20. function query($sSQL, $bDebugMode = FALSE)
  21. {
  22. if ($bDebugMode) {
  23. echo $sSQL."<br />";
  24. }
  25. $this->setCurrentResource(mysql_query($sSQL));
  26. if ($this->getNumberOfRows() == 0) {
  27. return false;
  28. }
  29. $this->setResultSet($this->getCurrentResource());
  30. return true;
  31. }
  32.  
  33. function getNumberOfRows()
  34. {
  35. $iRows = 0;
  36. $oResource = $this->getCurrentResource();
  37. if (@mysql_num_rows($oResource) > 0) {
  38. $iRows = mysql_num_rows($oResource);
  39. }
  40.  
  41. return $iRows;
  42. }
  43.  
  44. function selectInfo($sSQL, $bDebugMode = FALSE)
  45. {
  46. $aReturn = FALSE;
  47. $this->query($sSQL, $bDebugMode);
  48. if ($this->getNumberOfRows() > 0) {
  49. $aReturn = $this->getResultSet();
  50. }
  51. return $aReturn;
  52. }
  53.  
  54. function lastId()
  55. {
  56. return mysql_insert_id();
  57. }
  58.  
  59. function closeConnection()
  60. {
  61. mysql_close($this->oConn);
  62. }
  63.  
  64. function createSQLString($sInput)
  65. {
  66. $sInput = "'" . addslashes($sInput) . "'";
  67. return $sInput;
  68. }
  69.  
  70. function logToDatabaseViaArray($aFieldInfo, $sTableName, $sSQLConstraints = "", $sMethod = "INSERT")
  71. {
  72. $sFields = "";
  73. $sValues = "";
  74.  
  75. if ($sMethod == "UPDATE") {
  76. $sSQL = "UPDATE ".$sTableName." SET [** UPDATES **] ".$sSQLConstraints;
  77. $sUpdateString = "";
  78. foreach($aFieldInfo as $sKey => $sValue) {
  79. if (is_string($sValue)) {
  80. $sValue = $this->createSQLString($sValue);
  81. }
  82.  
  83. $sUpdateString .= $sKey . " = ".$sValue.", ";
  84. }
  85. $sUpdateString = substr($sUpdateString, 0, strlen($sUpdateString)-2);
  86. $sSQL = str_replace("[** UPDATES **]", $sUpdateString,$sSQL);
  87. $this->query($sSQL);
  88.  
  89. } else {
  90. $sSQL = "INSERT INTO ".$sTableName." ([** FIELDS **]) VALUES ([** VALUES **])";
  91.  
  92. foreach($aFieldInfo as $sKey => $sValue) {
  93. if (is_string($sValue)) {
  94. $sValue = $this->createSQLString($sValue);
  95. }
  96.  
  97. $sFields .= $sKey . ", ";
  98. $sValues .= $sValue . ", ";
  99. }
  100. $sFields = substr($sFields, 0, strlen($sFields)-2);
  101. $sValues = substr($sValues, 0, strlen($sValues)-2);
  102.  
  103. $sSQL = str_replace("[** FIELDS **]", $sFields,$sSQL);
  104.  
  105. $sSQL = str_replace("[** VALUES **]", $sValues,$sSQL);
  106. $this->query($sSQL);
  107. }
  108. }
  109.  
  110. function fetchResultsByQuery($sSQL, $bDebugMode = FALSE)
  111. {
  112. $this->query($sSQL, $bDebugMode);
  113. return $this->getResultSet();
  114. }
  115.  
  116. function getInsertId()
  117. {
  118. return mysql_insert_id($this->oConn);
  119. }
  120.  
  121. function destroyCurrentResource()
  122. {
  123. $oResource =& $this->getCurrentResource();
  124. if (is_object($oResource)) {
  125. mysql_free_result($oResource);
  126. }
  127. }
  128.  
  129. // MUTATORS & ACCESSORS
  130. function setCurrentResource($oNewResource)
  131. {
  132. $this->oCurrentResource = $oNewResource;
  133. }
  134. function getCurrentResource()
  135. {
  136. return $this->oCurrentResource;
  137. }
  138.  
  139. function setResultSet($oNewRecordSet)
  140. {
  141. $iLoop = 0;
  142. $this->aResultSet = array();
  143.  
  144. while($ROW = mysql_fetch_array($oNewRecordSet, MYSQL_ASSOC)) {
  145. while(list($key,$val) = each($ROW) ) {
  146. $this->aResultSet[$iLoop][$key] = $val;
  147. }
  148. $iLoop++;
  149. }
  150. if (count($this->aResultSet) > 0) {
  151. $this->setResultFound(TRUE);
  152. } else {
  153. $this->setResultFound(FALSE);
  154. }
  155. }
  156. function getResultSet()
  157. {
  158. if (isset($this->aResultSet)) {
  159. return $this->aResultSet;
  160. } else {
  161. return FALSE;
  162. }
  163. }
  164. function getResultRow($iRow=0)
  165. {
  166. if (isset($this->aResultSet)) {
  167. return $this->aResultSet[$iRow];
  168. } else {
  169. return FALSE;
  170. }
  171. }
  172.  
  173. function setResultFound($bNewStatus)
  174. {
  175. $this->bResultsFound = $bNewStatus;
  176. }
  177. function getResultFound()
  178. {
  179. return $this->bResultsFound;
  180. }
  181.  
  182. } // END CLASS
  183. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.