PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/noah_palm/server/ez_mysql.php

https://github.com/kjk/noah-palm
PHP | 410 lines | 247 code | 89 blank | 74 comment | 37 complexity | d596d55a6cd84b0b3c21ba127e5a3388 MD5 | raw file
  1. <?php
  2. // ==================================================================
  3. // Author: Justin Vincent (justin@visunet.ie)
  4. // Web: http://php.justinvincent.com
  5. // Name: ezSQL
  6. // Desc: Class to make it very easy to deal with mySQL database connections.
  7. // Taken from WordPress (Justin Vincent requires giving e-mail address
  8. // before downloading
  9. define('EZSQL_VERSION', '1.21');
  10. define('OBJECT', 'OBJECT', true);
  11. define('ARRAY_A', 'ARRAY_A', true);
  12. define('ARRAY_N', 'ARRAY_N', true);
  13. define('SAVEQUERIES', true);
  14. // The Main Class, renamed to avoid conflicts.
  15. class inoah_db {
  16. var $debug_called;
  17. var $vardump_called;
  18. var $show_errors = true;
  19. var $querycount;
  20. // ==================================================================
  21. // DB Constructor - connects to the server and selects a database
  22. function inoah_db($dbuser, $dbpassword, $dbname, $dbhost)
  23. {
  24. $this->dbh = @mysql_connect($dbhost,$dbuser,$dbpassword);
  25. if ( ! $this->dbh )
  26. {
  27. die("<div>
  28. <p><strong>Error establishing a database connection!</strong> This probably means that the connection information in youn <code>wp-config.php</code> file is incorrect. Double check it and try again.</p>
  29. <ul>
  30. <li>Are you sure you have the correct user/password?</li>
  31. <li>Are you sure that you have typed the correct hostname?</li>
  32. <li>Are you sure that the database server is running?</li>
  33. </ul>
  34. <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>
  35. </div>");
  36. }
  37. $this->select($dbname);
  38. $this->querycount = 0;
  39. }
  40. // ==================================================================
  41. // Select a DB (if another one needs to be selected)
  42. function select($db)
  43. {
  44. if ( !@mysql_select_db($db,$this->dbh))
  45. {
  46. die("
  47. <p>We're having a little trouble selecting the proper database for WordPress.</p>
  48. <ul>
  49. <li>Are you sure it exists?</li>
  50. <li>Your database name is currently specified as <code>" . DB_NAME ."</code>. Is this correct?</li>
  51. <li>On some systems the name of your database is prefixed with your username, so it would be like username_wordpress. Could that be the problem?</li>
  52. </ul>
  53. <p><a href='http://wordpress.org/support/'>WordPress Support Forums</a></p>");
  54. }
  55. }
  56. // ====================================================================
  57. // Format a string correctly for safe insert under all PHP conditions
  58. function escape($str)
  59. {
  60. return mysql_escape_string(stripslashes($str));
  61. }
  62. // ==================================================================
  63. // Print SQL/DB error.
  64. function print_error($str = '')
  65. {
  66. // All errors go to the global error array $EZSQL_ERROR..
  67. global $EZSQL_ERROR;
  68. // If no special error string then use mysql default..
  69. if ( !$str ) $str = mysql_error();
  70. // Log this error to the global array..
  71. $EZSQL_ERROR[] = array
  72. (
  73. 'query' => $this->last_query,
  74. 'error_str' => $str
  75. );
  76. // Is error output turned on or not..
  77. if ( $this->show_errors )
  78. {
  79. // If there is an error then take note of it
  80. print "<div id='error'>
  81. <p><strong>SQL/DB Error:</strong><br />
  82. [<span style='color: #007;'>$str</span>]<br />
  83. <code>$this->last_query</code></p>
  84. </div>";
  85. }
  86. else
  87. {
  88. return false;
  89. }
  90. }
  91. // ==================================================================
  92. // Turn error handling on or off..
  93. function show_errors()
  94. {
  95. $this->show_errors = true;
  96. }
  97. function hide_errors()
  98. {
  99. $this->show_errors = false;
  100. }
  101. // ==================================================================
  102. // Kill cached query results
  103. function flush()
  104. {
  105. // Get rid of these
  106. $this->last_result = null;
  107. $this->col_info = null;
  108. $this->last_query = null;
  109. }
  110. // ==================================================================
  111. // Basic Query - see docs for more detail
  112. function query($query)
  113. {
  114. // Flush cached values..
  115. $this->flush();
  116. // Log how the function was called
  117. $this->func_call = "\$db->query(\"$query\")";
  118. // Keep track of the last query for debug..
  119. $this->last_query = $query;
  120. // Perform the query via std mysql_query function..
  121. $this->result = mysql_query($query, $this->dbh);
  122. ++$this->querycount;
  123. if (SAVEQUERIES) {
  124. $this->savedqueries[] = $query;
  125. }
  126. // If there was an insert, delete or update see how many rows were affected
  127. // (Also, If there there was an insert take note of the insert_id
  128. $query_type = array('insert','delete','update','replace');
  129. // loop through the above array
  130. foreach ( $query_type as $word )
  131. {
  132. // This is true if the query starts with insert, delete or update
  133. if ( preg_match("/^\\s*$word /i",$query) )
  134. {
  135. $this->rows_affected = mysql_affected_rows();
  136. // This gets the insert ID
  137. if ( $word == 'insert' || $word == 'replace' )
  138. {
  139. $this->insert_id = mysql_insert_id($this->dbh);
  140. }
  141. $this->result = false;
  142. }
  143. }
  144. if ( mysql_error() )
  145. {
  146. // If there is an error then take note of it..
  147. $this->print_error();
  148. }
  149. else
  150. {
  151. // In other words if this was a select statement..
  152. if ( $this->result )
  153. {
  154. // =======================================================
  155. // Take note of column info
  156. $i=0;
  157. while ($i < @mysql_num_fields($this->result))
  158. {
  159. $this->col_info[$i] = @mysql_fetch_field($this->result);
  160. $i++;
  161. }
  162. // =======================================================
  163. // Store Query Results
  164. $i=0;
  165. while ( $row = @mysql_fetch_object($this->result) )
  166. {
  167. // Store relults as an objects within main array
  168. $this->last_result[$i] = $row;
  169. $i++;
  170. }
  171. // Log number of rows the query returned
  172. $this->num_rows = $i;
  173. @mysql_free_result($this->result);
  174. // If there were results then return true for $db->query
  175. if ( $i )
  176. {
  177. return true;
  178. }
  179. else
  180. {
  181. return false;
  182. }
  183. }
  184. else
  185. {
  186. // Update insert etc. was good..
  187. return true;
  188. }
  189. }
  190. }
  191. // ==================================================================
  192. // Get one variable from the DB - see docs for more detail
  193. function get_var($query=null, $x=0, $y=0)
  194. {
  195. // Log how the function was called
  196. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  197. // If there is a query then perform it if not then use cached results..
  198. if ( $query )
  199. {
  200. $this->query($query);
  201. }
  202. // Extract var out of cached results based x,y vals
  203. if ( $this->last_result[$y] )
  204. {
  205. $values = array_values(get_object_vars($this->last_result[$y]));
  206. }
  207. // If there is a value return it else return null
  208. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  209. }
  210. // ==================================================================
  211. // Get one row from the DB - see docs for more detail
  212. function get_row($query=null, $output=OBJECT, $y=0)
  213. {
  214. // Log how the function was called
  215. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  216. // If there is a query then perform it if not then use cached results..
  217. if ( $query )
  218. {
  219. $this->query($query);
  220. }
  221. // If the output is an object then return object using the row offset..
  222. if ( $output == OBJECT )
  223. {
  224. return $this->last_result[$y]?$this->last_result[$y]:null;
  225. }
  226. // If the output is an associative array then return row as such..
  227. elseif ( $output == ARRAY_A )
  228. {
  229. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  230. }
  231. // If the output is an numerical array then return row as such..
  232. elseif ( $output == ARRAY_N )
  233. {
  234. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  235. }
  236. // If invalid output type was specified..
  237. else
  238. {
  239. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  240. }
  241. }
  242. // ==================================================================
  243. // Function to get 1 column from the cached result set based in X index
  244. // se docs for usage and info
  245. function get_col($query=null,$x=0)
  246. {
  247. // If there is a query then perform it if not then use cached results..
  248. if ( $query )
  249. {
  250. $this->query($query);
  251. }
  252. // Extract the column values
  253. for ( $i=0; $i < count($this->last_result); $i++ )
  254. {
  255. $new_array[$i] = $this->get_var(null,$x,$i);
  256. }
  257. return $new_array;
  258. }
  259. // ==================================================================
  260. // Return the the query as a result set - see docs for more details
  261. function get_results($query=null, $output = OBJECT)
  262. {
  263. // Log how the function was called
  264. $this->func_call = "\$db->get_results(\"$query\", $output)";
  265. // If there is a query then perform it if not then use cached results..
  266. if ( $query )
  267. {
  268. $this->query($query);
  269. }
  270. // Send back array of objects. Each row is an object
  271. if ( $output == OBJECT )
  272. {
  273. return $this->last_result;
  274. }
  275. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  276. {
  277. if ( $this->last_result )
  278. {
  279. $i=0;
  280. foreach( $this->last_result as $row )
  281. {
  282. $new_array[$i] = get_object_vars($row);
  283. if ( $output == ARRAY_N )
  284. {
  285. $new_array[$i] = array_values($new_array[$i]);
  286. }
  287. $i++;
  288. }
  289. return $new_array;
  290. }
  291. else
  292. {
  293. return null;
  294. }
  295. }
  296. }
  297. // ==================================================================
  298. // Function to get column meta data info pertaining to the last query
  299. // see docs for more info and usage
  300. function get_col_info($info_type='name', $col_offset=-1)
  301. {
  302. if ( $this->col_info )
  303. {
  304. if ( $col_offset == -1 )
  305. {
  306. $i=0;
  307. foreach($this->col_info as $col )
  308. {
  309. $new_array[$i] = $col->{$info_type};
  310. $i++;
  311. }
  312. return $new_array;
  313. }
  314. else
  315. {
  316. return $this->col_info[$col_offset]->{$info_type};
  317. }
  318. }
  319. }
  320. }
  321. ?>