PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ez_sql.php

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