/includes/ez_sql_mysql.php

https://gitlab.com/billyprice1/Addon-Frontend · PHP · 337 lines · 145 code · 48 blank · 144 comment · 28 complexity · 8f94132699aeca0ffb8625f4fa804cf2 MD5 · raw file

  1. <?php
  2. /**
  3. * ezSQL Database specific class - mySQL
  4. * Desc..: mySQL component (part of ezSQL databse abstraction library)
  5. *
  6. * @author Justin Vincent (jv@jvmultimedia.com)
  7. * @author Stefanie Janine Stoelting (mail@stefanie-stoelting.de)
  8. * @link http://twitter.com/justinvincent
  9. * @name ezSQL_mysql
  10. * @package ezSQL
  11. * @license FREE / Donation (LGPL - You may do what you like with ezSQL - no exceptions.)
  12. *
  13. */
  14. class ezSQL_mysql extends ezSQLcore
  15. {
  16. /*
  17. * ezSQL error strings - mySQL
  18. * @var array
  19. */
  20. private $ezsql_mysql_str = array
  21. (
  22. 1 => 'Require $dbuser and $dbpassword to connect to a database server',
  23. 2 => 'Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running?',
  24. 3 => 'Require $dbname to select a database',
  25. 4 => 'mySQL database connection is not active',
  26. 5 => 'Unexpected error while trying to select database'
  27. );
  28. /**
  29. * Database user name
  30. * @var string
  31. */
  32. private $dbuser;
  33. /**
  34. * Database password for the given user
  35. * @var string
  36. */
  37. private $dbpassword;
  38. /**
  39. * Database name
  40. * @var string
  41. */
  42. private $dbname;
  43. /**
  44. * Host name or IP address
  45. * @var string
  46. */
  47. private $dbhost;
  48. /**
  49. * Database charset
  50. * @var string Default is utf8
  51. */
  52. private $charset = 'utf8';
  53. /**
  54. * Show errors
  55. * @var boolean Default is true
  56. */
  57. public $show_errors = true;
  58. /**
  59. * Constructor - allow the user to perform a qucik connect at the same time
  60. * as initialising the ezSQL_mysql class
  61. *
  62. * @param string $dbuser The database user name
  63. * @param string $dbpassword The database users password
  64. * @param string $dbname The name of the database
  65. * @param string $dbhost The host name or IP address of the database server.
  66. * Default is localhost
  67. * @param string $charset The database charset
  68. * Default is empty string
  69. */
  70. public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $charset='') {
  71. if ( ! function_exists ('mysql_connect') ) {
  72. throw new Exception('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
  73. }
  74. if ( ! class_exists ('ezSQLcore') ) {
  75. throw new Exception('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
  76. }
  77. parent::__construct();
  78. $this->dbuser = $dbuser;
  79. $this->dbpassword = $dbpassword;
  80. $this->dbname = $dbname;
  81. $this->dbhost = $dbhost;
  82. if ( ! empty($charset) ) {
  83. $this->charset = $charset;
  84. }
  85. } // __construct
  86. /**
  87. * Short hand way to connect to mssql database server and select a mssql
  88. * database at the same time
  89. *
  90. * @param string $dbuser The database user name
  91. * @param string $dbpassword The database users password
  92. * @param string $dbname The name of the database
  93. * @param string $dbhost The host name or IP address of the database server.
  94. * Default is localhost
  95. * @return boolean
  96. */
  97. public function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost') {
  98. if ( ! $this->connect($dbuser, $dbpassword, $dbhost, true) ) ;
  99. else if ( ! $this->select($dbname) ) ;
  100. return $this->connected;
  101. } // quick_connect
  102. /**
  103. * Try to connect to mySQL database server
  104. *
  105. * @param string $dbuser The database user name
  106. * @param string $dbpassword The database users password
  107. * @param string $dbhost The host name or IP address of the database server.
  108. * Default is localhost
  109. * @param type $charset The database charset
  110. * Default is empty string
  111. * @return boolean
  112. */
  113. public function connect($dbuser='', $dbpassword='', $dbhost='localhost', $charset='') {
  114. $this->connected = false;
  115. $this->dbuser = empty($dbuser) ? $this->dbuser : $dbuser;
  116. $this->dbpassword = empty($dbpassword) ? $this->dbpassword : $dbpassword;
  117. $this->dbhost = $dbhost!='localhost' ? $this->dbhost : $dbhost;
  118. $this->charset = empty($charset) ? $this->charset : $charset;
  119. // Must have a user and a password
  120. if ( empty($this->dbuser) ) {
  121. $this->register_error($this->ezsql_mysql_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  122. $this->show_errors ? trigger_error($this->ezsql_mysql_str[1], E_USER_WARNING) : null;
  123. } else if ( ! $this->dbh = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpassword, true, 131074) ) {
  124. // Try to establish the server database handle
  125. $this->register_error($this->ezsql_mysql_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  126. $this->show_errors ? trigger_error($this->ezsql_mysql_str[2], E_USER_WARNING) : null;
  127. } else {
  128. mysql_set_charset($this->charset, $this->dbh);
  129. $this->connected = true;
  130. }
  131. return $this->connected;
  132. } // connect
  133. /**
  134. * Try to select a mySQL database
  135. *
  136. * @param string $dbname The name of the database
  137. * @return boolean
  138. */
  139. public function select($dbname='') {
  140. if ( ! $dbname ) {
  141. // Must have a database name
  142. $this->register_error($this->ezsql_mysql_str[3] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  143. $this->show_errors ? trigger_error($this->ezsql_mysql_str[3], E_USER_WARNING) : null;
  144. } else if ( ! $this->dbh ) {
  145. // Must have an active database connection
  146. $this->register_error($this->ezsql_mysql_str[4] . ' in ' . __FILE__ . ' on line ' . __LINE__);
  147. $this->show_errors ? trigger_error($this->ezsql_mysql_str[4], E_USER_WARNING) : null;
  148. } else if ( !@mysql_select_db($dbname, $this->dbh) ) {
  149. // Try to connect to the database
  150. // Try to get error supplied by mysql if not use our own
  151. if ( !$str = @mysql_error($this->dbh)) {
  152. $str = $this->ezsql_mysql_str[5];
  153. }
  154. $this->register_error($str . ' in ' .__FILE__ . ' on line ' . __LINE__);
  155. $this->show_errors ? trigger_error($str, E_USER_WARNING) : null;
  156. } else {
  157. $this->dbname = $dbname;
  158. $this->connected = true;
  159. }
  160. return $this->connected;
  161. } // select
  162. /**
  163. * Format a mySQL string correctly for safe mySQL insert
  164. * (no matter if magic quotes are on or not)
  165. *
  166. * @param string $str
  167. * @return string
  168. */
  169. public function escape($str) {
  170. return mysql_real_escape_string(stripslashes($str));
  171. } // escape
  172. /**
  173. * Return mySQL specific system date syntax
  174. * i.e. Oracle: SYSDATE Mysql: NOW()
  175. *
  176. * @return string
  177. */
  178. public function sysdate() {
  179. return 'NOW()';
  180. } // sysdate
  181. /**
  182. * Perform mySQL query and try to determine result value
  183. *
  184. * @param type $query
  185. * @return boolean
  186. */
  187. public function query($query) {
  188. // Initialise return
  189. $return_val = 0;
  190. // Flush cached values..
  191. $this->flush();
  192. // For reg expressions
  193. $query = trim($query);
  194. // Log how the function was called
  195. $this->func_call = "\$db->query(\"$query\")";
  196. // Keep track of the last query for debug..
  197. $this->last_query = $query;
  198. // Count how many queries there have been
  199. $this->num_queries++;
  200. // Use core file cache function
  201. if ( $cache = $this->get_cache($query) ) {
  202. return $cache;
  203. }
  204. // If there is no existing database connection then try to connect
  205. if ( ! isset($this->dbh) || ! $this->dbh ) {
  206. $this->connect($this->dbuser, $this->dbpassword, $this->dbhost);
  207. $this->select($this->dbname);
  208. }
  209. // Perform the query via std mysql_query function..
  210. $this->result = @mysql_query($query,$this->dbh);
  211. // If there is an error then take note of it..
  212. if ( $str = @mysql_error($this->dbh) ) {
  213. $is_insert = true;
  214. $this->register_error($str);
  215. $this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
  216. return false;
  217. }
  218. // Query was an insert, delete, update, replace
  219. $is_insert = false;
  220. if ( preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
  221. $this->affectedRows = @mysql_affected_rows($this->dbh);
  222. // Take note of the insert_id
  223. if ( preg_match("/^(insert|replace)\s+/i", $query) ) {
  224. $this->insert_id = @mysql_insert_id($this->dbh);
  225. }
  226. // Return number fo rows affected
  227. $return_val = $this->affectedRows;
  228. } else {
  229. // Query was a select
  230. // Take note of column info
  231. $i=0;
  232. while ($i < @mysql_num_fields($this->result)) {
  233. $this->col_info[$i] = @mysql_fetch_field($this->result);
  234. $i++;
  235. }
  236. // Store Query Results
  237. $num_rows=0;
  238. while ( $row = @mysql_fetch_object($this->result) ) {
  239. // Store relults as an objects within main array
  240. $this->last_result[$num_rows] = $row;
  241. $num_rows++;
  242. }
  243. @mysql_free_result($this->result);
  244. // Log number of rows the query returned
  245. $this->num_rows = $num_rows;
  246. // Return number of rows selected
  247. $return_val = $this->num_rows;
  248. }
  249. // disk caching of queries
  250. $this->store_cache($query, $is_insert);
  251. // If debug ALL queries
  252. $this->trace || $this->debug_all ? $this->debug() : null ;
  253. return $return_val;
  254. } // query
  255. /**
  256. * Close the database connection
  257. */
  258. public function disconnect() {
  259. if ( $this->dbh ) {
  260. mysql_close($this->dbh);
  261. $this->connected = false;
  262. }
  263. $this->connected = false;
  264. } // function
  265. /**
  266. * Returns the current database server host
  267. *
  268. * @return string
  269. */
  270. public function getDBHost() {
  271. return $this->dbhost;
  272. } // getDBHost
  273. /**
  274. * Returns the current connection charset
  275. *
  276. * @return string
  277. */
  278. public function getCharset() {
  279. return $this->charset;
  280. } // getCharset
  281. /**
  282. * Returns the last inserted autoincrement
  283. *
  284. * @return int
  285. */
  286. public function getInsertId() {
  287. return mysql_insert_id();
  288. } // getInsertId
  289. } // ezSQL_mysql