PageRenderTime 59ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/Database.php

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