PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/code/apps/myop/api/lib/ez_sql_mysql.php

http://thinksns-2.googlecode.com/
PHP | 270 lines | 150 code | 55 blank | 65 comment | 24 complexity | e4fa883375277a64fd7e21464eb7e8cd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**********************************************************************
  3. * Author: Justin Vincent (justin@visunet.ie)
  4. * Web...: http://php.justinvincent.com
  5. * Name..: ezSQL_mysql
  6. * Desc..: mySQL component (part of ezSQL databse abstraction library)
  7. * Help..: http://justinvincent.com/docs/ezsql/ez_sql_help.htm
  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. mysql_query('SET NAMES UTF8',$this->dbh);
  53. return $return_val;
  54. }
  55. /**********************************************************************
  56. * Try to connect to mySQL database server
  57. */
  58. function connect($dbuser='', $dbpassword='', $dbhost='localhost')
  59. {
  60. global $ezsql_mysql_str; $return_val = false;
  61. // Must have a user and a password
  62. if ( ! $dbuser )
  63. {
  64. $this->register_error($ezsql_mysql_str[1].' in '.__FILE__.' on line '.__LINE__);
  65. $this->show_errors ? trigger_error($ezsql_mysql_str[1],E_USER_WARNING) : null;
  66. }
  67. // Try to establish the server database handle
  68. else if ( ! $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword,true) )
  69. {
  70. $this->register_error($ezsql_mysql_str[2].' in '.__FILE__.' on line '.__LINE__);
  71. $this->show_errors ? trigger_error($ezsql_mysql_str[2],E_USER_WARNING) : null;
  72. }
  73. else
  74. {
  75. $this->dbuser = $dbuser;
  76. $this->dbpassword = $dbpassword;
  77. $this->dbhost = $dbhost;
  78. $return_val = true;
  79. }
  80. mysql_query('SET NAMES UTF8',$this->dbh);
  81. return $return_val;
  82. }
  83. /**********************************************************************
  84. * Try to select a mySQL database
  85. */
  86. function select($dbname='')
  87. {
  88. global $ezsql_mysql_str; $return_val = false;
  89. // Must have a database name
  90. if ( ! $dbname )
  91. {
  92. $this->register_error($ezsql_mysql_str[3].' in '.__FILE__.' on line '.__LINE__);
  93. $this->show_errors ? trigger_error($ezsql_mysql_str[3],E_USER_WARNING) : null;
  94. }
  95. // Must have an active database connection
  96. else if ( ! $this->dbh )
  97. {
  98. $this->register_error($ezsql_mysql_str[4].' in '.__FILE__.' on line '.__LINE__);
  99. $this->show_errors ? trigger_error($ezsql_mysql_str[4],E_USER_WARNING) : null;
  100. }
  101. // Try to connect to the database
  102. else if ( !@mysql_select_db($dbname,$this->dbh) )
  103. {
  104. // Try to get error supplied by mysql if not use our own
  105. if ( !$str = @mysql_error($this->dbh))
  106. $str = $ezsql_mysql_str[5];
  107. $this->register_error($str.' in '.__FILE__.' on line '.__LINE__);
  108. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  109. }
  110. else
  111. {
  112. $this->dbname = $dbname;
  113. $return_val = true;
  114. }
  115. return $return_val;
  116. }
  117. /**********************************************************************
  118. * Format a mySQL string correctly for safe mySQL insert
  119. * (no mater if magic quotes are on or not)
  120. */
  121. function escape($str)
  122. {
  123. return mysql_escape_string(stripslashes($str));
  124. }
  125. /**********************************************************************
  126. * Return mySQL specific system date syntax
  127. * i.e. Oracle: SYSDATE Mysql: NOW()
  128. */
  129. function sysdate()
  130. {
  131. return 'NOW()';
  132. }
  133. /**********************************************************************
  134. * Perform mySQL query and try to detirmin result value
  135. */
  136. function query($query)
  137. {
  138. // Initialise return
  139. $return_val = 0;
  140. // Flush cached values..
  141. $this->flush();
  142. // For reg expressions
  143. $query = trim($query);
  144. // Log how the function was called
  145. $this->func_call = "\$db->query(\"$query\")";
  146. // Keep track of the last query for debug..
  147. $this->last_query = $query;
  148. // Count how many queries there have been
  149. $this->num_queries++;
  150. // Use core file cache function
  151. if ( $cache = $this->get_cache($query) )
  152. {
  153. return $cache;
  154. }
  155. // If there is no existing database connection then try to connect
  156. if ( ! isset($this->dbh) || ! $this->dbh )
  157. {
  158. $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  159. $this->select($this->dbname);
  160. }
  161. // Perform the query via std mysql_query function..
  162. $this->result = @mysql_query($query,$this->dbh);
  163. // If there is an error then take note of it..
  164. if ( $str = @mysql_error($this->dbh) )
  165. {
  166. $is_insert = true;
  167. $this->register_error($str);
  168. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  169. return false;
  170. }
  171. // Query was an insert, delete, update, replace
  172. $is_insert = false;
  173. if ( preg_match("/^(insert|delete|update|replace)\s+/i",$query) )
  174. {
  175. $this->rows_affected = @mysql_affected_rows();
  176. // Take note of the insert_id
  177. if ( preg_match("/^(insert|replace)\s+/i",$query) )
  178. {
  179. $this->insert_id = @mysql_insert_id($this->dbh);
  180. }
  181. // Return number fo rows affected
  182. $return_val = $this->rows_affected;
  183. }
  184. // Query was a select
  185. else
  186. {
  187. // Take note of column info
  188. $i=0;
  189. while ($i < @mysql_num_fields($this->result))
  190. {
  191. $this->col_info[$i] = @mysql_fetch_field($this->result);
  192. $i++;
  193. }
  194. // Store Query Results
  195. $num_rows=0;
  196. while ( $row = @mysql_fetch_object($this->result) )
  197. {
  198. // Store relults as an objects within main array
  199. $this->last_result[$num_rows] = $row;
  200. $num_rows++;
  201. }
  202. @mysql_free_result($this->result);
  203. // Log number of rows the query returned
  204. $this->num_rows = $num_rows;
  205. // Return number of rows selected
  206. $return_val = $this->num_rows;
  207. }
  208. // disk caching of queries
  209. $this->store_cache($query,$is_insert);
  210. // If debug ALL queries
  211. $this->trace || $this->debug_all ? $this->debug() : null ;
  212. return $return_val;
  213. }
  214. }
  215. ?>