Advertisement
Guest User

Untitled

a guest
Jun 15th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. <?php
  2. /**
  3. * Copyright (c) 2010 Nabeel Shahzad
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14.  
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. *
  23. * @author Nabeel Shahzad
  24. * @copyright Copyright (c) 2008-2010, Nabeel Shahzad
  25. * @link http://github.com/nshahzad/ezdb
  26. * @license MIT License
  27. */
  28.  
  29. include_once dirname(__FILE__).'/ezdb_base.class.php';
  30.  
  31. class ezDB_mysql extends ezDB_Base
  32. {
  33.  
  34. /**
  35. * Constructor, connects to database immediately, unless $dbname is blank
  36. *
  37. * @param string $dbuser Database username
  38. * @param string $dbpassword Database password
  39. * @param string $dbname Database name (if blank, will not connect)
  40. * @param string $dbhost Hostname, optional, default is 'localhost'
  41. * @return bool Connect status
  42. *
  43. */
  44. public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  45. {
  46. if($dbname == '') return false;
  47.  
  48. parent::__construct();
  49.  
  50. if($this->connect($dbuser, $dbpassword, $dbhost))
  51. {
  52. return $this->select($dbname);
  53. }
  54.  
  55. return false;
  56. }
  57.  
  58. /**
  59. * Explicitly close the connection on destruct
  60. */
  61.  
  62. public function __destruct()
  63. {
  64. $this->close();
  65. }
  66.  
  67. /**
  68. * Connects to database immediately, unless $dbname is blank
  69. *
  70. * @param string $dbuser Database username
  71. * @param string $dbpassword Database password
  72. * @param string $dbname Database name (if blank, will not connect)
  73. * @param string $dbhost Hostname, optional, default is 'localhost'
  74. * @return bool Connect status
  75. *
  76. */
  77. public function quick_connect($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
  78. {
  79. $this->__construct($dbuser, $dbpassword, $dbname, $dbhost);
  80. }
  81.  
  82.  
  83. /**
  84. * Connect to MySQL, but not to a database
  85. *
  86. * @param string $dbuser Username
  87. * @param string $dbpassword Password
  88. * @param string $dbhost Host, optional, default is localhost
  89. * @return bool Success
  90. *
  91. */
  92. public function connect($dbuser='', $dbpassword='', $dbhost='localhost')
  93. {
  94. $this->dbh = @mysql_connect($dbhost, $dbuser, $dbpassword, true);
  95.  
  96. if(!$this->dbh)
  97. {
  98. if($this->throw_exceptions)
  99. throw new ezDB_Error(mysql_error(), mysql_errno());
  100.  
  101. $this->register_error(mysql_error(), mysql_errno());
  102. return false;
  103. }
  104.  
  105. $this->clear_errors();
  106. return true;
  107. }
  108.  
  109. /**
  110. * Select a MySQL Database
  111. *
  112. * @param string $dbname Database name
  113. * @return bool Success or not
  114. *
  115. */
  116. public function select($dbname='')
  117. {
  118. // Must have a database name
  119. if ($dbname == '')
  120. {
  121. if($this->throw_exceptions)
  122. throw new ezDB_Error('No database specified!', -1);
  123.  
  124. $this->register_error('No database name specified!');
  125. return false;
  126. }
  127. // Must have an active database connection
  128. if (!$this->dbh)
  129. {
  130. if($this->throw_exceptions)
  131. throw new ezDB_Error('Invalid or inactive connection!');
  132.  
  133. $this->register_error('Can\'t select database, invalid or inactive connection', -1);
  134. return false;
  135. }
  136.  
  137. if(!mysql_select_db($dbname, $this->dbh))
  138. {
  139. if($this->throw_exceptions)
  140. throw new ezDB_Error(mysql_error(), mysql_errno());
  141.  
  142. $this->register_error(mysql_error($this->dbh), mysql_errno($this->dbh));
  143. return false;
  144. }
  145.  
  146. $this->clear_errors();
  147. return true;
  148. }
  149.  
  150. /**
  151. * Close the database connection
  152. */
  153. public function close()
  154. {
  155. return @mysql_close($this->dbh);
  156. }
  157.  
  158. /**
  159. * Format a mySQL string correctly for safe mySQL insert
  160. * (no matter if magic quotes are on or not)
  161. *
  162. * @param string $str String to escape
  163. * @return string Returns the escaped string
  164. *
  165. */
  166. public function escape($str)
  167. {
  168. return mysql_real_escape_string(stripslashes($str), $this->dbh);
  169. }
  170.  
  171.  
  172. /**
  173. * Returns the DB specific timestamp function (Oracle: SYSDATE, MySQL: NOW())
  174. *
  175. * @return string Timestamp function
  176. *
  177. */
  178. public function sysdate()
  179. {
  180. return 'NOW()';
  181. }
  182.  
  183. /**
  184. * Run the SQL query, and get the result. Returns false on failure
  185. * Check $this->error() and $this->errno() functions for any errors
  186. * MySQL returns errno() == 0 for no error. That's the most reliable check
  187. *
  188. * @param string $query SQL Query
  189. * @return mixed Return values
  190. *
  191. */
  192. public function query($query)
  193. {
  194. // Flush cached values..
  195. $this->flush();
  196.  
  197. // For reg expressions
  198. $query = trim($query);
  199. $this->last_query = $query;
  200.  
  201. // Count how many queries there have been
  202. $this->num_queries++;
  203.  
  204. // Reset ourselves
  205. $this->clear_errors();
  206.  
  207. // Use core file cache function
  208. if($cache = $this->get_cache($query))
  209. {
  210. return $cache;
  211. }
  212.  
  213. // Make sure connection is ALIVEE!
  214. if (!$this->dbh )
  215. {
  216. if($this->throw_exceptions)
  217. throw new ezDB_Error(mysql_error(), mysql_errno());
  218.  
  219. $this->register_error('There is no active database connection!');
  220. return false;
  221. }
  222.  
  223. // Perform the query via std mysql_query function..
  224. $this->result = mysql_query($query, $this->dbh);
  225.  
  226. // If there is an error then take note of it..
  227. if(!$this->result && mysql_errno($this->dbh) != 0)
  228. {
  229. // Something went wrong
  230. if($this->throw_exceptions)
  231. throw new ezDB_Error(mysql_error(), mysql_errno(), $query);
  232.  
  233. $this->register_error(mysql_error(), mysql_errno());
  234. return false;
  235. }
  236. else
  237. {
  238. $this->clear_errors();
  239. }
  240.  
  241. // Query was an insert, delete, update, replace
  242. $is_insert = false;
  243. if(preg_match("/^(insert|delete|update|replace)\s+/i",$query))
  244. {
  245. $this->rows_affected = mysql_affected_rows($this->dbh);
  246. $this->num_rows = $this->rows_affected;
  247.  
  248. $insert_id = mysql_insert_id($this->dbh);
  249. if($insert_id > 0)
  250. {
  251. $this->insert_id = $insert_id;
  252. $is_insert = true;
  253. }
  254.  
  255. // Return number fo rows affected
  256. $return_val = $this->rows_affected;
  257. }
  258. // Query was a select
  259. else
  260. {
  261. // Take note of column info
  262. $i=0;
  263. $num_rows = 0;
  264. if(is_resource($this->result))
  265. {
  266. while ($i < mysql_num_fields($this->result))
  267. {
  268. $this->col_info[$i] = mysql_fetch_field($this->result);
  269. $i++;
  270. }
  271.  
  272. // Store Query Results
  273. while($row = mysql_fetch_object($this->result))
  274. {
  275. // Store relults as an objects within main array
  276. $this->last_result[$num_rows] = $row;
  277. $num_rows++;
  278. }
  279.  
  280. mysql_free_result($this->result);
  281. }
  282.  
  283. // Log number of rows the query returned
  284. $this->rows_affected = $num_rows;
  285. $this->num_rows = $num_rows;
  286.  
  287. // Return number of rows selected
  288. $return_val = $this->num_rows;
  289. }
  290.  
  291. // disk caching of queries
  292. $this->store_cache($query,$is_insert);
  293.  
  294. // If debug ALL queries
  295. $this->trace || $this->debug_all ? $this->debug() : null ;
  296.  
  297. return $return_val;
  298. }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement