PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/movil/libs/ezdb1-simple.php

https://bitbucket.org/jonarano/joneame
PHP | 382 lines | 212 code | 75 blank | 95 comment | 54 complexity | 1a0d278c10a34964196bd45487c21672 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. // IMPORTANT: this is a reduced version of ezdb1.php
  3. // Its purpose is SPEED, only SPEED, use ezdb1.php if you want debug information.
  4. // YOU HAVE BEEEN WARNED.
  5. // -- ricardo galli
  6. //
  7. // ==================================================================
  8. // Author: Justin Vincent (justin@visunet.ie)
  9. // Web: http://php.justinvincent.com
  10. // Name: ezSQL
  11. // Desc: Class to make it very easy to deal with mySQL database connections.
  12. //
  13. // !! IMPORTANT !!
  14. //
  15. // Please send me a mail telling me what you think of ezSQL
  16. // and what your using it for!! Cheers. [ justin@visunet.ie ]
  17. //
  18. // ==================================================================
  19. // User Settings -- CHANGE HERE
  20. //define("EZSQL_DB_USER", $globals['db_user']); // <-- mysql db user
  21. //define("EZSQL_DB_PASSWORD", $globals['db_password']); // <-- mysql db password
  22. //define("EZSQL_DB_NAME", $globals['db_name']); // <-- mysql db pname
  23. //define("EZSQL_DB_HOST", $globals['db_server']); // <-- mysql server host
  24. // ==================================================================
  25. // ezSQL Constants
  26. define("EZSQL_VERSION","1.5");
  27. // ==================================================================
  28. // The Main Class
  29. class db {
  30. var $show_errors = true;
  31. var $num_queries = 0;
  32. var $col_info;
  33. var $dbuser;
  34. var $dbpassword;
  35. var $dbname;
  36. var $dbhost;
  37. var $dbmaster;
  38. var $persistent;
  39. var $master_persistent;
  40. var $dbh_update = false;
  41. var $dbh_select = false;
  42. var $dbh = false;
  43. // ==================================================================
  44. // DB Constructor - connects to the server and selects a database
  45. function db($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $dbmaster=false) {
  46. $this->dbuser = $dbuser;
  47. $this->dbpassword = $dbpassword;
  48. $this->dbname = $dbname;
  49. $this->dbhost = $dbhost;
  50. $this->dbmaster = $dbmaster;
  51. if ( $dbmaster && $dbmaster != $dbhost ) {
  52. $this->dbmaster = $dbmaster;
  53. } else {
  54. $this->dbmaster = false;
  55. }
  56. }
  57. // Reset the connection to the slave if it was using the master
  58. function barrier() {
  59. if ($this->dbh && $this->dbh !== $this->dbh_select) {
  60. if ($this->dbh_select) {
  61. $this->dbh = & $this->dbh_select;
  62. } else {
  63. $this->connect();
  64. }
  65. }
  66. }
  67. function connect($master = false) {
  68. if ($master && $this->dbmaster ) {
  69. if ($this->dbh_update) {
  70. $this->dbh = & $this->dbh_update;
  71. return;
  72. } else {
  73. if ($this->master_persistent) {
  74. $this->dbh_update = @mysql_pconnect($this->dbmaster, $this->dbuser,$this->dbpassword, MYSQL_CLIENT_COMPRESS);
  75. } else {
  76. $this->dbh_update = @mysql_connect($this->dbmaster, $this->dbuser,$this->dbpassword, MYSQL_CLIENT_COMPRESS);
  77. }
  78. $this->dbh = & $this->dbh_update;
  79. }
  80. } else {
  81. if ($this->persistent) {
  82. $this->dbh_select = @mysql_pconnect($this->dbhost, $this->dbuser,$this->dbpassword);
  83. } else {
  84. $this->dbh_select = @mysql_connect($this->dbhost, $this->dbuser,$this->dbpassword);
  85. }
  86. $this->dbh = & $this->dbh_select;
  87. }
  88. if ( ! $this->dbh ) {
  89. include '../db-lounge.php';
  90. die;
  91. }
  92. if (!empty($this->dbname)) $this->select($this->dbname);
  93. $this->query("SET NAMES 'utf8'");
  94. }
  95. // Garbitu kontsulta
  96. function garbitu($var, $quotes = true) {
  97. if (is_array($var)) { //run each array item through this function (by reference)
  98. foreach ($var as &$val) {
  99. $val = $this->sanitize($val);
  100. }
  101. }
  102. else if (is_string($var)) { //clean strings
  103. $var = mysql_real_escape_string($var);
  104. if ($quotes) {
  105. $var = "'". $var ."'";
  106. }
  107. }
  108. else if (is_null($var)) { //convert null variables to SQL NULL
  109. $var = "NULL";
  110. }
  111. else if (is_bool($var)) { //convert boolean variables to binary boolean
  112. $var = ($var) ? 1 : 0;
  113. }
  114. return $var;
  115. }
  116. // ==================================================================
  117. // Select a DB (if another one needs to be selected)
  118. function select($db) {
  119. if (!$this->dbh) $this->connect();
  120. if ( !@mysql_select_db($db,$this->dbh)) {
  121. $this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
  122. }
  123. }
  124. // ====================================================================
  125. // Format a string correctly for safe insert under all PHP conditions
  126. function escape($str) {
  127. if (!$this->dbh) $this->connect();
  128. return mysql_real_escape_string(stripslashes($str), $this->dbh);
  129. }
  130. // ==================================================================
  131. // Print SQL/DB error.
  132. function print_error($str = "") {
  133. // All erros go to the global error array $EZSQL_ERROR..
  134. global $EZSQL_ERROR;
  135. if (!$this->dbh) $this->connect();
  136. // If no special error string then use mysql default..
  137. if ( !$str ) {
  138. $str = mysql_error($this->dbh);
  139. $error_no = mysql_errno($this->dbh);
  140. }
  141. // Log this error to the global array..
  142. $EZSQL_ERROR[] = array
  143. (
  144. "error_str" => $str,
  145. "error_no" => $error_no
  146. );
  147. // Is error output turned on or not..
  148. if ( $this->show_errors ) {
  149. // If there is an error then take note of it
  150. print "<blockquote><font face=arial size=2 color=ff0000>";
  151. print "<b>SQL/DB Error --</b> ";
  152. print "[<font color=000077>$str</font>]";
  153. print "</font></blockquote>";
  154. } else {
  155. return false;
  156. }
  157. }
  158. // ==================================================================
  159. // Turn error handling on or off..
  160. function show_errors() {
  161. $this->show_errors = true;
  162. }
  163. function hide_errors() {
  164. $this->show_errors = false;
  165. }
  166. // ==================================================================
  167. // Kill cached query results
  168. function flush() {
  169. // Get rid of these
  170. $this->last_result = null;
  171. $this->col_info = null;
  172. }
  173. // ==================================================================
  174. // Basic Query - see docs for more detail
  175. function query($query) {
  176. // For reg expressions
  177. $query = trim($query);
  178. $is_update = preg_match("/^(insert|delete|update|replace)\s+/i",$query);
  179. if (!$this->dbh || ($is_update && ! $this->dbh_update !== $this->dbh) ) $this->connect($is_update);
  180. // initialise return
  181. $return_val = 0;
  182. // Flush cached values..
  183. $this->flush();
  184. // Perform the query via std mysql_query function..
  185. $this->result = @mysql_query($query,$this->dbh);
  186. $this->num_queries++;
  187. // If there is an error then take note of it..
  188. if ( mysql_error() ) {
  189. $this->print_error();
  190. return false;
  191. }
  192. // Query was an insert, delete, update, replace
  193. if ( $is_update ) {
  194. $this->rows_affected = mysql_affected_rows();
  195. // Take note of the insert_id
  196. if ( preg_match("/^(insert|replace)\s+/i",$query) ) {
  197. $this->insert_id = mysql_insert_id($this->dbh);
  198. }
  199. // Return number fo rows affected
  200. $return_val = $this->rows_affected;
  201. } else {
  202. // Query was an select
  203. /*
  204. *** WARN WARN
  205. *** We don't use col info
  206. *** Commented out meanwhile
  207. // Take note of column info
  208. $i=0;
  209. while ($i < @mysql_num_fields($this->result)) {
  210. $this->col_info[$i] = @mysql_fetch_field($this->result);
  211. $i++;
  212. }
  213. */
  214. // Store Query Results
  215. $num_rows=0;
  216. while ( $row = @mysql_fetch_object($this->result) ) {
  217. // Store relults as an objects within main array
  218. $this->last_result[$num_rows] = $row;
  219. $num_rows++;
  220. }
  221. @mysql_free_result($this->result);
  222. // Log number of rows the query returned
  223. $this->num_rows = $num_rows;
  224. // Return number of rows selected
  225. $return_val = $this->num_rows;
  226. }
  227. return $return_val;
  228. }
  229. // ==================================================================
  230. // Get one variable from the DB - see docs for more detail
  231. function get_var($query=null,$x=0,$y=0) {
  232. // If there is a query then perform it if not then use cached results..
  233. if ( $query ) {
  234. $this->query($query);
  235. }
  236. // Extract var out of cached results based x,y vals
  237. if ( $this->last_result[$y] ) {
  238. $values = array_values(get_object_vars($this->last_result[$y]));
  239. }
  240. // If there is a value return it else return null
  241. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  242. }
  243. // ==================================================================
  244. // Get one row from the DB - see docs for more detail
  245. function get_object($query,$class) {
  246. $this->result = @mysql_query($query,$this->dbh);
  247. // If there is an error then take note of it..
  248. if ( mysql_error() ) {
  249. $this->print_error();
  250. return false;
  251. }
  252. $object = @mysql_fetch_object($this->result, $class);
  253. @mysql_free_result($this->result);
  254. return $object?$object:null;
  255. }
  256. // ==================================================================
  257. // Get one row from the DB - see docs for more detail
  258. function get_row($query=null,$y=0) {
  259. // If there is a query then perform it if not then use cached results..
  260. if ( $query ) {
  261. $this->query($query);
  262. }
  263. return $this->last_result[$y]?$this->last_result[$y]:null;
  264. }
  265. // ==================================================================
  266. // Function to get 1 column from the cached result set based in X index
  267. // se docs for usage and info
  268. function get_col($query=null,$x=0) {
  269. // If there is a query then perform it if not then use cached results..
  270. if ( $query ) {
  271. $this->query($query);
  272. }
  273. // Extract the column values
  274. for ( $i=0; $i < count($this->last_result); $i++ ) {
  275. $new_array[$i] = $this->get_var(null,$x,$i);
  276. }
  277. return $new_array;
  278. }
  279. // ==================================================================
  280. // Return the the query as a result set - see docs for more details
  281. function get_results($query=null) {
  282. // If there is a query then perform it if not then use cached results..
  283. if ( $query ) {
  284. $this->query($query);
  285. }
  286. // Send back array of objects. Each row is an object
  287. return $this->last_result;
  288. }
  289. // ==================================================================
  290. // Function to get column meta data info pertaining to the last query
  291. // see docs for more info and usage
  292. function get_col_info($info_type="name",$col_offset=-1) {
  293. if ( $this->col_info ) {
  294. if ( $col_offset == -1 ) {
  295. $i=0;
  296. foreach($this->col_info as $col ) {
  297. $new_array[$i] = $col->{$info_type};
  298. $i++;
  299. }
  300. return $new_array;
  301. } else {
  302. return $this->col_info[$col_offset]->{$info_type};
  303. }
  304. }
  305. }
  306. }
  307. ?>