PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jvs10-sortable-bd/lib/ez_sql_mysql.php

http://jvstutoriales.googlecode.com/
PHP | 265 lines | 148 code | 52 blank | 65 comment | 24 complexity | 1893edc1f1d2b8943dfb0658d391e873 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, GPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**********************************************************************
  3. * Author: Justin Vincent (jv@jvmultimedia.com)
  4. * Web...: http://twitter.com/justinvincent
  5. * Name..: ezSQL_mysql
  6. * Desc..: mySQL component (part of ezSQL databse abstraction library)
  7. *
  8. */
  9. /**********************************************************************
  10. * ezSQL error strings - mySQL
  11. */
  12. $ezsql_mysql_str = array
  13. (
  14. 1 => 'Require $dbuser and $dbpassword to connect to a database server',
  15. 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
  16. 3 => 'Require $dbname to select a database',
  17. 4 => 'mySQL database connection is not active',
  18. 5 => 'Unexpected error while trying to select database'
  19. );
  20. /**********************************************************************
  21. * ezSQL Database specific class - mySQL
  22. */
  23. if ( ! function_exists ('mysql_connect') ) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
  24. if ( ! class_exists ('ezSQLcore') ) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  25. class ezSQL_mysql extends ezSQLcore
  26. {
  27. var $dbuser = false;
  28. var $dbpassword = false;
  29. var $dbname = false;
  30. var $dbhost = false;
  31. /**********************************************************************
  32. * Constructor - allow the user to perform a qucik connect at the
  33. * same time as initialising the ezSQL_mysql class
  34. */
  35. function ezSQL_mysql($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  36. {
  37. $this->dbuser = $dbuser;
  38. $this->dbpassword = $dbpassword;
  39. $this->dbname = $dbname;
  40. $this->dbhost = $dbhost;
  41. }
  42. /**********************************************************************
  43. * Short hand way to connect to mySQL database server
  44. * and select a mySQL database at the same time
  45. */
  46. function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  47. {
  48. $return_val = false;
  49. if ( ! $this->connect($dbuser, $dbpassword, $dbhost,true) ) ;
  50. else if ( ! $this->select($dbname) ) ;
  51. else $return_val = true;
  52. return $return_val;
  53. }
  54. /**********************************************************************
  55. * Try to connect to mySQL database server
  56. */
  57. function connect($dbuser='', $dbpassword='', $dbhost='localhost')
  58. {
  59. global $ezsql_mysql_str; $return_val = false;
  60. // Must have a user and a password
  61. if ( ! $dbuser )
  62. {
  63. $this->register_error($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
  64. $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
  65. }
  66. // Try to establish the server database handle
  67. else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true) )
  68. {
  69. $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
  70. $this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
  71. }
  72. else
  73. {
  74. $this->dbuser = $dbuser;
  75. $this->dbpassword = $dbpassword;
  76. $this->dbhost = $dbhost;
  77. $return_val = true;
  78. }
  79. return $return_val;
  80. }
  81. /**********************************************************************
  82. * Try to select a mySQL database
  83. */
  84. function select($dbname='')
  85. {
  86. global $ezsql_mysql_str; $return_val = false;
  87. // Must have a database name
  88. if ( ! $dbname )
  89. {
  90. $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
  91. $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
  92. }
  93. // Must have an active database connection
  94. else if ( ! $this->dbh )
  95. {
  96. $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
  97. $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
  98. }
  99. // Try to connect to the database
  100. else if ( !@mysql_select_db($dbname,$this->dbh) )
  101. {
  102. // Try to get error supplied by mysql if not use our own
  103. if ( !$str = @mysql_error($this->dbh))
  104. $str = $ezsql_mysql_str[5];
  105. $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
  106. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  107. }
  108. else
  109. {
  110. $this->dbname = $dbname;
  111. $return_val = true;
  112. }
  113. return $return_val;
  114. }
  115. /**********************************************************************
  116. * Format a mySQL string correctly for safe mySQL insert
  117. * (no mater if magic quotes are on or not)
  118. */
  119. function escape($str)
  120. {
  121. return mysql_real_escape_string(stripslashes($str));
  122. }
  123. /**********************************************************************
  124. * Return mySQL specific system date syntax
  125. * i.e. Oracle: SYSDATE Mysql: NOW()
  126. */
  127. function sysdate()
  128. {
  129. return 'NOW()';
  130. }
  131. /**********************************************************************
  132. * Perform mySQL query and try to detirmin result value
  133. */
  134. function query($query)
  135. {
  136. // Initialise return
  137. $return_val = 0;
  138. // Flush cached values..
  139. $this->flush();
  140. // For reg expressions
  141. $query = trim($query);
  142. // Log how the function was called
  143. $this->func_call = "\$db->query(\"$query\")";
  144. // Keep track of the last query for debug..
  145. $this->last_query = $query;
  146. // Count how many queries there have been
  147. $this->num_queries++;
  148. // Use core file cache function
  149. if ( $cache = $this->get_cache($query) )
  150. {
  151. return $cache;
  152. }
  153. // If there is no existing database connection then try to connect
  154. if ( ! isset($this->dbh) || ! $this->dbh )
  155. {
  156. $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  157. $this->select($this->dbname);
  158. }
  159. // Perform the query via std mysql_query function..
  160. $this->result = @mysql_query($query,$this->dbh);
  161. // If there is an error then take note of it..
  162. if ( $str = @mysql_error($this->dbh) )
  163. {
  164. $is_insert = true;
  165. $this->register_error($str);
  166. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  167. return false;
  168. }
  169. // Query was an insert, delete, update, replace
  170. $is_insert = false;
  171. if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
  172. {
  173. $this->rows_affected = @mysql_affected_rows();
  174. // Take note of the insert_id
  175. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  176. {
  177. $this->insert_id = @mysql_insert_id($this->dbh);
  178. }
  179. // Return number fo rows affected
  180. $return_val = $this->rows_affected;
  181. }
  182. // Query was a select
  183. else
  184. {
  185. // Take note of column info
  186. $i=0;
  187. while ($i < @mysql_num_fields($this->result))
  188. {
  189. $this->col_info[$i] = @mysql_fetch_field($this->result);
  190. $i++;
  191. }
  192. // Store Query Results
  193. $num_rows=0;
  194. while ( $row = @mysql_fetch_object($this->result) )
  195. {
  196. // Store relults as an objects within main array
  197. $this->last_result[$num_rows] = $row;
  198. $num_rows++;
  199. }
  200. @mysql_free_result($this->result);
  201. // Log number of rows the query returned
  202. $this->num_rows = $num_rows;
  203. // Return number of rows selected
  204. $return_val = $this->num_rows;
  205. }
  206. // disk caching of queries
  207. $this->store_cache($query,$is_insert);
  208. // If debug ALL queries
  209. $this->trace || $this->debug_all ? $this->debug() : null ;
  210. return $return_val;
  211. }
  212. }
  213. ?>