PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/ festos/core/MySQL.php

http://festos.googlecode.com/
PHP | 209 lines | 75 code | 18 blank | 116 comment | 10 complexity | 21aaacb0d64bf72f628501a840cfbfda MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package SPLIB
  4. * @version $Id: MySQL.php,v 1.1 2003/12/12 08:06:07 kevin Exp $
  5. */
  6. /**
  7. * MySQL Database Connection Class
  8. * @access public
  9. * @package SPLIB
  10. */
  11. class MySQL {
  12. /**
  13. * MySQL server hostname
  14. * @access private
  15. * @var string
  16. */
  17. var $host;
  18. /**
  19. * MySQL username
  20. * @access private
  21. * @var string
  22. */
  23. var $dbUser;
  24. /**
  25. * MySQL user's password
  26. * @access private
  27. * @var string
  28. */
  29. var $dbPass;
  30. /**
  31. * Name of database to use
  32. * @access private
  33. * @var string
  34. */
  35. var $dbName;
  36. /**
  37. * MySQL Resource link identifier stored here
  38. * @access private
  39. * @var string
  40. */
  41. var $dbConn;
  42. /**
  43. * True/false error switch
  44. * @access private
  45. * @var Boolean
  46. */
  47. var $connectError;
  48. /**
  49. * Stores error messages
  50. * @access private
  51. * @var string
  52. */
  53. var $errorMsg;
  54. /**
  55. * MySQL constructor
  56. * @param string host (MySQL server hostname)
  57. * @param string dbUser (MySQL User Name)
  58. * @param string dbPass (MySQL User Password)
  59. * @param string dbName (Database to select)
  60. * @access public
  61. */
  62. function MySQL ($host,$dbUser,$dbPass,$dbName) {
  63. $this->host=$host;
  64. $this->dbUser=$dbUser;
  65. $this->dbPass=$dbPass;
  66. $this->dbName=$dbName;
  67. $this->errorMsg='';
  68. $this->connectToDb();
  69. }
  70. /**
  71. * Establishes connection to MySQL and selects a database
  72. * @return void
  73. * @access private
  74. */
  75. function connectToDb () {
  76. // Make connection to MySQL server
  77. if (!$this->dbConn = @mysql_connect($this->host,
  78. $this->dbUser,
  79. $this->dbPass)) {
  80. $this->connectError=true;
  81. $this->errorMsg .= 'Could not connect to server';
  82. // Select database
  83. } else if ( !@mysql_select_db($this->dbName,$this->dbConn) ) {
  84. $this->connectError=true;
  85. $this->errorMsg .= 'Could not select database';
  86. }
  87. }
  88. /**
  89. * Checks for MySQL errors
  90. * @return boolean
  91. * @access public
  92. */
  93. function isError () {
  94. if ( $this->connectError )
  95. return true;
  96. $this->errorMsg .= mysql_error ($this->dbConn);
  97. if ( empty ($error) )
  98. return false;
  99. else
  100. return true;
  101. }
  102. /**
  103. * Returns an instance of MySQLResult to fetch rows with
  104. * @param $sql string the database query to run
  105. * @return MySQLResult
  106. * @access public
  107. */
  108. function & query($sql) {
  109. if (!$queryResource=mysql_query($sql,$this->dbConn))
  110. trigger_error ('Query failed: '.mysql_error($this->dbConn).
  111. ' SQL: '.$sql);
  112. return new MySQLResult($this,$queryResource);
  113. }
  114. }
  115. /**
  116. * MySQLResult Data Fetching Class
  117. * @access public
  118. * @package SPLIB
  119. */
  120. class MySQLResult {
  121. /**
  122. * Instance of MySQL providing database connection
  123. * @access private
  124. * @var MySQL
  125. */
  126. var $mysql;
  127. /**
  128. * Query resource
  129. * @access private
  130. * @var resource
  131. */
  132. var $query;
  133. /**
  134. * MySQLResult constructor
  135. * @param object mysql (instance of MySQL class)
  136. * @param resource query (MySQL query resource)
  137. * @access public
  138. */
  139. function MySQLResult(& $mysql,$query) {
  140. $this->mysql=& $mysql;
  141. $this->query=$query;
  142. }
  143. /**
  144. * Fetches a rowFROM ".$config['dbprefix']."the result
  145. * @return array
  146. * @access public
  147. */
  148. function fetch () {
  149. if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) ) {
  150. return $row;
  151. } else if ( $this->size() > 0 ) {
  152. mysql_data_seek($this->query,0);
  153. return false;
  154. } else {
  155. return false;
  156. }
  157. }
  158. /**
  159. * Returns the number of rows selected
  160. * @return int
  161. * @access public
  162. */
  163. function size () {
  164. return mysql_num_rows($this->query);
  165. }
  166. /**
  167. * Returns the number of rows affected by a DELETE, INSERT, REPLACE, or UPDATE statement
  168. * @return int
  169. * @access public
  170. */
  171. function affected () {
  172. return mysql_affected_rows($this->mysql->dbConn);
  173. }
  174. /**
  175. * Returns the ID of the last row inserted
  176. * @return int
  177. * @access public
  178. */
  179. function insertID () {
  180. return mysql_insert_id($this->mysql->dbConn);
  181. }
  182. /**
  183. * Checks for MySQL errors
  184. * @return boolean
  185. * @access public
  186. */
  187. function isError () {
  188. return $this->mysql->isError();
  189. }
  190. }
  191. ?>