PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/classes/database.php

#
PHP | 240 lines | 71 code | 28 blank | 141 comment | 9 complexity | bde3f8e02cb070227654f6c1aa3e5c36 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. *
  4. * Copyright (C) 2010 Tom Kaczocha <freedomdeveloper@yahoo.com>
  5. *
  6. * This file is part of LaMantengo.
  7. *
  8. * LaMantengo is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LaMantengo is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LaMantengo. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * My_Sql_Database class is a template for database connection objects.
  24. *
  25. * Copyright (c) 2010 Tom Kaczocha
  26. *
  27. * @package
  28. * @author Tom Kaczocha <freedomdeveloper@yahoo.com>
  29. * @copyright 2010 Tom Kaczocha
  30. * @license GNU General Public License, version 3.0
  31. * @version 2.0
  32. * @access public
  33. */
  34. class My_Sql_Database {
  35. /**
  36. * Magic Quotes Active Flag
  37. *
  38. * @access private
  39. * @var String
  40. */
  41. private $_mMagicQuotesActive;
  42. /**
  43. * Real Escape String Exists Flag
  44. *
  45. * @access private
  46. * @var String
  47. */
  48. private $_mRealEscapeStringExists;
  49. /**
  50. * Connection Flag
  51. *
  52. * @access private
  53. * @var String
  54. */
  55. private $_mConnection;
  56. /**
  57. * Last Query
  58. *
  59. * @access public
  60. * @var String
  61. */
  62. public $mLastquery;
  63. /**
  64. * Database Constructor
  65. *
  66. * @param
  67. * @access public
  68. *
  69. * @return
  70. *
  71. */
  72. public function __construct() {
  73. $this->openConnection();
  74. $this->_mMagicQuotesActive = get_magic_quotes_gpc();
  75. $this->_mRealEscapeStringExists = function_exists("mysql_real_escape_string");
  76. }
  77. /**
  78. * Function opens a connection to the database
  79. *
  80. * @param
  81. * @access public
  82. *
  83. * @return
  84. *
  85. */
  86. public function openConnection() {
  87. // Make the _mConnection.
  88. $this->_mConnection = mysql_connect(SERVER, DB_USER, DB_PASSWORD);
  89. if (!$this->_mConnection) {
  90. die('Could not connect to MYSQL: ' . mysql_error());
  91. }
  92. else {
  93. // Select the database
  94. $db_select = mysql_select_db(DB_NAME, $this->_mConnection);
  95. if (!$db_select) {
  96. die('Could not select the database: ' . mysql_error());
  97. }
  98. }
  99. }
  100. /**
  101. * Function performs database query
  102. *
  103. * @param String $query Query
  104. * @access public
  105. *
  106. * @return String Result
  107. *
  108. */
  109. public function query($query) {
  110. $this->mLastquery = $query;
  111. $result = mysql_query($query, $this->_mConnection);
  112. $this->confirmQuery($result);
  113. return $result;
  114. }
  115. /**
  116. * Function closes last database connection
  117. *
  118. * @param
  119. * @access public
  120. *
  121. * @return
  122. *
  123. */
  124. public function closeConnection() {
  125. if (isset($this->_mConnection)) {
  126. mysql_close($this->_mConnection);
  127. unset($this->_mConnection);
  128. }
  129. }
  130. /**
  131. * Function prepares query values for input
  132. *
  133. * @param String $value Query
  134. * @access public
  135. *
  136. * @return String Query
  137. *
  138. */
  139. public function escapeValue($value) {
  140. if ($_mRealEscapeStringExists) {
  141. if ($this->_mMagicQuotesActive) {
  142. $value = stripslashes($value);
  143. }
  144. $value = mysql_real_escape_string($value);
  145. }
  146. else {
  147. // if magic quotes aren't already on then add slashes manually
  148. if (!$this->_mMagicQuotesActive) {
  149. $value = addslashes($value);
  150. }
  151. // if magic quotes are active, then the slashes already exist
  152. }
  153. return trim($value);
  154. }
  155. /**
  156. * Function retrieves data from a query result
  157. *
  158. * @param String $result Result
  159. * @access public
  160. *
  161. * @return DataSet Dataset
  162. *
  163. */
  164. public function fetchArray($result) {
  165. return mysql_fetch_array($result);
  166. }
  167. /**
  168. * Function retrieves the number of rows from resultset
  169. *
  170. * @param String $result_set ResultSet
  171. * @access public
  172. *
  173. * @return String Number of Rows
  174. *
  175. */
  176. public function numRows($result_set) {
  177. return mysql_num_rows($result_set);
  178. }
  179. /**
  180. * Function gets the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query
  181. *
  182. * @param
  183. * @access public
  184. *
  185. * @return String Number of Rows
  186. *
  187. */
  188. public function affectedRows() {
  189. return mysql_affected_rows($this->_mConnection);
  190. }
  191. /**
  192. * Function confirms query
  193. *
  194. * @param $result ResultSet
  195. * @access private
  196. *
  197. * @return
  198. *
  199. */
  200. private function confirmQuery($result) {
  201. if (!$result) {
  202. $output = 'Database query failed: ' . mysql_error() . '<br /><br /';
  203. $output .= 'Last SQL query: ' . $this->mLastquery; // debugging message
  204. die($output);
  205. }
  206. }
  207. public function getLastID() {
  208. return mysql_insert_id();
  209. }
  210. }
  211. $database = new My_Sql_Database();
  212. ?>