PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/ez_sql.php

https://github.com/vykintasv/psiprogresas
PHP | 420 lines | 243 code | 105 blank | 72 comment | 34 complexity | 9de331ca673de293f4771b508a299801 MD5 | raw file
  1. <?php
  2. // ==================================================================
  3. // Author: Justin Vincent (justin@visunet.ie)
  4. // Web: http://www.justinvincent.com
  5. // Name: ezSQL
  6. // Desc: Class to make it very easy to deal with mySQL database connections.
  7. // ==================================================================
  8. // User Settings -- CHANGE HERE
  9. // ==================================================================
  10. // ezSQL Constants
  11. define("EZSQL_VERSION","1.01");
  12. define("OBJECT","OBJECT",true);
  13. define("ARRAY_A","ARRAY_A",true);
  14. define("ARRAY_N","ARRAY_N",true);
  15. // ==================================================================
  16. // The Main Class
  17. class db {
  18. // ==================================================================
  19. // DB Constructor - connects to the server and selects a database
  20. function db($dbuser, $dbpassword, $dbname, $dbhost)
  21. {
  22. $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
  23. if ( ! $this->dbh )
  24. {
  25. $this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
  26. return false;
  27. }
  28. return $this->select($dbname);
  29. }
  30. // ==================================================================
  31. // Select a DB (if another one needs to be selected)
  32. function select($db)
  33. {
  34. if ( !@mysql_select_db($db,$this->dbh))
  35. {
  36. $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>");
  37. return false;
  38. }
  39. return true;
  40. }
  41. // ==================================================================
  42. // Print SQL/DB error.
  43. function print_error($str = "")
  44. {
  45. if ( !$str ) $str = mysql_error();
  46. // If there is an error then take note of it
  47. print "<blockquote><font face=arial size=2 color=ff0000>";
  48. print "<b>SQL/DB Error --</b> ";
  49. print "[<font color=000077>$str</font>]";
  50. print "</font></blockquote>";
  51. }
  52. // ==================================================================
  53. // Basic Query - see docs for more detail
  54. function query($query, $output = OBJECT)
  55. {
  56. // Log how the function was called
  57. $this->func_call = "\$db->query(\"$query\", $output)";
  58. // Kill this
  59. $this->last_result = null;
  60. $this->col_info = null;
  61. // Keep track of the last query for debug..
  62. $this->last_query = $query;
  63. // Perform the query via std mysql_query function..
  64. $this->result = mysql_query($query,$this->dbh);
  65. if ( mysql_error() )
  66. {
  67. // If there is an error then take note of it..
  68. $this->print_error();
  69. }
  70. else
  71. {
  72. // In other words if this was a select statement..
  73. if ( $this->result )
  74. {
  75. // =======================================================
  76. // Take note of column info
  77. $i=0;
  78. while ($i < @mysql_num_fields($this->result))
  79. {
  80. $this->col_info[$i] = @mysql_fetch_field($this->result);
  81. $i++;
  82. }
  83. // =======================================================
  84. // Store Query Results
  85. $i=0;
  86. while ( $row = @mysql_fetch_object($this->result) )
  87. {
  88. // Store relults as an objects within main array
  89. $this->last_result[$i] = $row;
  90. $i++;
  91. }
  92. @mysql_free_result($this->result);
  93. // If there were results then return true for $db->query
  94. if ( $i )
  95. {
  96. return true;
  97. }
  98. else
  99. {
  100. return false;
  101. }
  102. }
  103. }
  104. }
  105. // ==================================================================
  106. // Get one variable from the DB - see docs for more detail
  107. function get_var($query=null,$x=0,$y=0)
  108. {
  109. // Log how the function was called
  110. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  111. // If there is a query then perform it if not then use cached results..
  112. if ( $query )
  113. {
  114. $this->query($query);
  115. }
  116. // Extract var out of cached results based x,y vals
  117. if ( $this->last_result[$y] )
  118. {
  119. $values = array_values(get_object_vars($this->last_result[$y]));
  120. }
  121. // If there is a value return it else return null
  122. return $values[$x]?$values[$x]:null;
  123. }
  124. // ==================================================================
  125. // Get one row from the DB - see docs for more detail
  126. function get_row($query=null,$y=0,$output=OBJECT)
  127. {
  128. // Log how the function was called
  129. $this->func_call = "\$db->get_row(\"$query\",$y,$output)";
  130. // If there is a query then perform it if not then use cached results..
  131. if ( $query )
  132. {
  133. $this->query($query);
  134. }
  135. // If the output is an object then return object using the row offset..
  136. if ( $output == OBJECT )
  137. {
  138. return $this->last_result[$y]?$this->last_result[$y]:null;
  139. }
  140. // If the output is an associative array then return row as such..
  141. elseif ( $output == ARRAY_A )
  142. {
  143. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  144. }
  145. // If the output is an numerical array then return row as such..
  146. elseif ( $output == ARRAY_N )
  147. {
  148. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  149. }
  150. // If invalid output type was specified..
  151. else
  152. {
  153. $this->print_error(" \$db->get_row(string query,int offset,output type) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N ");
  154. }
  155. }
  156. // ==================================================================
  157. // Function to get 1 column from the cached result set based in X index
  158. // se docs for usage and info
  159. function get_col($query=null,$x=0)
  160. {
  161. // If there is a query then perform it if not then use cached results..
  162. if ( $query )
  163. {
  164. $this->query($query);
  165. }
  166. // Extract the column values
  167. for ( $i=0; $i < count($this->last_result); $i++ )
  168. {
  169. $new_array[$i] = $this->get_var(null,$x,$i);
  170. }
  171. return $new_array;
  172. }
  173. // ==================================================================
  174. // Return the the query as a result set - see docs for more details
  175. function get_results($query=null, $output = OBJECT)
  176. {
  177. // Log how the function was called
  178. $this->func_call = "\$db->get_results(\"$query\", $output)";
  179. // If there is a query then perform it if not then use cached results..
  180. if ( $query )
  181. {
  182. $this->query($query);
  183. }
  184. // Send back array of objects. Each row is an object
  185. if ( $output == OBJECT )
  186. {
  187. return $this->last_result;
  188. }
  189. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  190. {
  191. if ( $this->last_result )
  192. {
  193. $i=0;
  194. foreach( $this->last_result as $row )
  195. {
  196. $new_array[$i] = get_object_vars($row);
  197. if ( $output == ARRAY_N )
  198. {
  199. $new_array[$i] = array_values($new_array[$i]);
  200. }
  201. $i++;
  202. }
  203. return $new_array;
  204. }
  205. else
  206. {
  207. return null;
  208. }
  209. }
  210. }
  211. // ==================================================================
  212. // Function to get column meta data info pertaining to the last query
  213. // see docs for more info and usage
  214. function get_col_info($info_type="name",$col_offset=-1)
  215. {
  216. if ( $this->col_info )
  217. {
  218. if ( $col_offset == -1 )
  219. {
  220. $i=0;
  221. foreach($this->col_info as $col )
  222. {
  223. $new_array[$i] = $col->{$info_type};
  224. $i++;
  225. }
  226. return $new_array;
  227. }
  228. else
  229. {
  230. return $this->col_info[$col_offset]->{$info_type};
  231. }
  232. }
  233. }
  234. // ==================================================================
  235. // Dumps the contents of any input variable to screen in a nicely
  236. // formatted and easy to understand way - any type: Object, Var or Array
  237. function vardump($mixed)
  238. {
  239. echo "<blockquote><font color=000090>";
  240. echo "<pre><font face=arial>";
  241. if ( ! $this->vardump_called )
  242. {
  243. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  244. }
  245. print_r($mixed);
  246. echo "\n\n<b>Last Query:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  247. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  248. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  249. echo "</font></pre></font></blockquote>";
  250. echo "\n<hr size=1 noshade color=dddddd>";
  251. $this->vardump_called = true;
  252. }
  253. // Alias for the above function
  254. function dumpvars($mixed)
  255. {
  256. $this->vardump($mixed);
  257. }
  258. // ==================================================================
  259. // Displays the last query string that was sent to the database & a
  260. // table listing results (if there were any).
  261. // (abstracted into a seperate file to save server overhead).
  262. function debug()
  263. {
  264. echo "<blockquote>";
  265. // Only show ezSQL credits once..
  266. if ( ! $this->debug_called )
  267. {
  268. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  269. }
  270. echo "<font face=arial size=2 color=000099><b>Query --</b> ";
  271. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  272. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  273. echo "<blockquote>";
  274. if ( $this->col_info )
  275. {
  276. // =====================================================
  277. // Results top rows
  278. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  279. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  280. for ( $i=0; $i < count($this->col_info); $i++ )
  281. {
  282. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}<br><font size=2><b>{$this->col_info[$i]->name}</b></font></td>";
  283. }
  284. echo "</tr>";
  285. // ======================================================
  286. // print main results
  287. if ( $this->last_result )
  288. {
  289. $i=0;
  290. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  291. {
  292. $i++;
  293. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  294. foreach ( $one_row as $item )
  295. {
  296. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  297. }
  298. echo "</tr>";
  299. }
  300. } // if last result
  301. else
  302. {
  303. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  304. }
  305. echo "</table>";
  306. } // if col_info
  307. else
  308. {
  309. echo "<font face=arial size=2>No Results</font>";
  310. }
  311. echo "</blockquote></blockquote><hr noshade color=dddddd size=1>";
  312. $this->debug_called = true;
  313. }
  314. }
  315. ?>