PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/ezdb/ezDB_Base.class.php

https://github.com/richard4339/threatlevel
PHP | 1005 lines | 529 code | 160 blank | 316 comment | 80 complexity | 2a2ade109e86390bbc1d47b85abc18bc MD5 | raw file
  1. <?php
  2. /**
  3. * Codon PHP Framework
  4. * www.nsslive.net/codon
  5. * Software License Agreement (BSD License)
  6. *
  7. * Copyright (c) 2008 Nabeel Shahzad, nsslive.net
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. The name of the author may not be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * @author Nabeel Shahzad
  34. * @copyright Copyright (c) 2008, Nabeel Shahzad
  35. * @link http://www.nsslive.net/codon
  36. * @license BSD License
  37. *
  38. * Based on ezDB, By Justin Vincent (justin@visunet.ie)
  39. * Web...: http://php.justinvincent.com
  40. */
  41. /**********************************************************************
  42. *
  43. * Name..: ezDB
  44. * Desc..: ezDB Core module - database abstraction library to make
  45. * it very easy to deal with databases.
  46. *
  47. */
  48. /**********************************************************************
  49. * ezDB Constants
  50. */
  51. define('EZSQL_VERSION','2.03');
  52. define('OBJECT','OBJECT', true);
  53. define('ARRAY_A','ARRAY_A', true);
  54. define('ARRAY_N','ARRAY_N', true);
  55. /**
  56. * Simple exception container class
  57. *
  58. */
  59. class ezDB_Error extends Exception
  60. {
  61. /* Use these to keep it consistant with ezDB_Base */
  62. public $error;
  63. public $errno;
  64. public $last_query = '';
  65. public function __construct($message, $code, $query='')
  66. {
  67. parent::__construct($message, $code);
  68. $this->last_query = $query;
  69. $this->error = $message;
  70. $this->errno = $code;
  71. }
  72. public function __toString()
  73. {
  74. $html .= '<blockquote>';
  75. $html .= '<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>'.
  76. $this->message . ' (' . $this->code . ')</b></font>]<br />';
  77. $html .= '[<font color=000000><b>'.$this->last_query.'</b></font>]</font><p>
  78. </blockquote><hr noshade color=dddddd size=1>';
  79. return $html;
  80. }
  81. }
  82. /**
  83. * Base class for ezDB
  84. *
  85. */
  86. class ezDB_Base
  87. {
  88. public $trace = false; // same as $debug_all
  89. public $debug_all = false; // same as $trace
  90. public $debug_called = false;
  91. public $vardump_called = false;
  92. public $show_errors = true;
  93. public $num_queries = 0;
  94. public $last_query = null;
  95. public $error = null;
  96. public $errno = null;
  97. public $col_info = null;
  98. public $captured_errors = array();
  99. public $insert_id;
  100. public $table_prefix = '';
  101. public $dbuser = false;
  102. public $dbpassword = false;
  103. public $dbname = false;
  104. public $dbhost = false;
  105. public $result;
  106. public $default_type = OBJECT;
  107. public $get_col_info = false;
  108. public $debug_echo_is_on = true;
  109. public $throw_exceptions = true;
  110. /* These settings are handled by __set() below, but can still
  111. be called as $db->cache_type = '...';, just under-go some
  112. checking to make sure that they're valid */
  113. protected $settings = array(
  114. 'cache_type' => 'file',
  115. );
  116. public $cache_timeout = 3600; # In seconds
  117. public $cache_dir = false; # Directory to cache to if using 'file'
  118. public $cache_queries = false;
  119. public $cache_inserts = false;
  120. public $use_disk_cache = false;
  121. protected $last_result;
  122. public function __construct()
  123. {
  124. # Check for memcache support
  125. }
  126. public function __set($name, $value)
  127. {
  128. switch($name)
  129. {
  130. case 'cache_type':
  131. $this->set_cache_type($value);
  132. break;
  133. default:
  134. $this->settings[$name] = $value;
  135. break;
  136. }
  137. }
  138. public function __get($name)
  139. {
  140. return $this->settings[$name];
  141. }
  142. /**
  143. * Clear any previous errors
  144. */
  145. public function clear_errors()
  146. {
  147. $this->error = '';
  148. $this->errno = 0;
  149. }
  150. /**
  151. * Set a table prefix for quick_*() functions
  152. *
  153. * @param string $prefix Table prefix
  154. * @return none
  155. *
  156. */
  157. public function set_table_prefix($prefix='')
  158. {
  159. $this->table_prefix = $prefix;
  160. }
  161. /**
  162. * Set the option to throw exceptions or not
  163. *
  164. * @param bool $bool True or false
  165. * @return none
  166. *
  167. */
  168. public function throw_exceptions($bool=true)
  169. {
  170. $this->throw_exceptions = $bool;
  171. }
  172. /**
  173. * Enable or disable caching, can be set per-query
  174. *
  175. * @param bool $bool True/False
  176. * @return none
  177. */
  178. public function set_caching($bool)
  179. {
  180. if($bool === true)
  181. {
  182. $this->cache_query = true;
  183. $this->use_disk_cache = true;
  184. }
  185. else
  186. {
  187. $this->cache_query = false;
  188. $this->use_disk_cache = false;
  189. }
  190. }
  191. /**
  192. * Set the cache type (file, memcache, check)
  193. *
  194. * @param string $type Caching type
  195. * @return none
  196. *
  197. */
  198. public function set_cache_type($type)
  199. {
  200. if($type == 'memcache')
  201. {
  202. $this->settings['cache_type'] = 'file'; # Not enabled for now
  203. if($this->throw_exceptions)
  204. throw new ezDB_Error('memcache not available', -1);
  205. return false;
  206. }
  207. elseif($type == 'apc')
  208. {
  209. if(!function_exists('apc_add'))
  210. {
  211. $this->settings['cache_type'] = 'file';
  212. if($this->throw_exceptions)
  213. throw new ezDB_Error('apc not available', -1);
  214. return false;
  215. }
  216. }
  217. else
  218. {
  219. # Default to file if they selected anythign weird
  220. $this->settings['cache_type'] = 'file';
  221. }
  222. return true;
  223. }
  224. /**
  225. * Set the path of the cache
  226. *
  227. * @param mixed $path This is a description
  228. * @return mixed This is the return value description
  229. *
  230. */
  231. public function set_cache_dir($path)
  232. {
  233. $this->cache_dir = $path;
  234. }
  235. public function set_cache_timeout($timeout)
  236. {
  237. $this->cache_timeout= $timeout;
  238. }
  239. /**
  240. * Save an error that occurs in our log
  241. *
  242. * @param string $err_str This is the error string
  243. * @param int $err_no This is the error number
  244. * @return bool True
  245. *
  246. */
  247. public function register_error($err_str, $err_no=-1)
  248. {
  249. // Keep track of last error
  250. $this->error = $err_str;
  251. $this->errno = $err_no;
  252. // Capture all errors to an error array no matter what happens
  253. $this->captured_errors[] = array(
  254. 'error' => $err_str,
  255. 'errno' => $err_no,
  256. 'query' => $this->last_query);
  257. //show output if enabled
  258. //$this->show_errors ? trigger_error($this->error . '(' . $this->last_query . ')', E_USER_WARNING) : null;
  259. }
  260. /**
  261. * Get the error log from all the query
  262. *
  263. * @return array Queries and their error/errno values
  264. *
  265. */
  266. public function get_all_errors()
  267. {
  268. return $this->captured_errors;
  269. }
  270. /**
  271. * Returns the error string from the previous query
  272. *
  273. * @return string Error string
  274. *
  275. */
  276. public function error()
  277. {
  278. return $this->error;
  279. }
  280. /**
  281. * Returns the error code from the previous query
  282. *
  283. * @return mixed Error code
  284. *
  285. */
  286. public function errno()
  287. {
  288. return $this->errno;
  289. }
  290. /**
  291. * Show all errors by default
  292. *
  293. * @return bool true
  294. *
  295. */
  296. public function show_errors()
  297. {
  298. $this->show_errors = true;
  299. return true;
  300. }
  301. /**
  302. * Hide any errors from showing by default.
  303. * Can also access the property as $this->show_errors=false
  304. *
  305. * @return bool true
  306. *
  307. */
  308. public function hide_errors()
  309. {
  310. $this->show_errors = false;
  311. return true;
  312. }
  313. /**
  314. * Remove the results from the last query
  315. *
  316. * @return bool Returns true
  317. *
  318. */
  319. public function flush()
  320. {
  321. // Get rid of these
  322. $this->last_result = null;
  323. $this->col_info = null;
  324. $this->last_query = null;
  325. $this->from_disk_cache = false;
  326. return true;
  327. }
  328. /**
  329. * Get a single column/variable
  330. *
  331. * @param string $query SQL query
  332. * @param int $x Column offset (default 0, returns first column)
  333. * @param int $y Row offset (default 0, first row returned)
  334. * @return mixed Returns the value of the variable
  335. *
  336. */
  337. public function get_var($query=null,$x=0,$y=0)
  338. {
  339. // Log how the function was called
  340. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  341. // If there is a query then perform it if not then use cached results..
  342. if ( $query )
  343. {
  344. $this->query($query);
  345. }
  346. // Extract var out of cached results based x,y vals
  347. if ( $this->last_result[$y] )
  348. {
  349. $values = array_values(get_object_vars($this->last_result[$y]));
  350. }
  351. // If there is a value return it else return null
  352. return (isset($values[$x]) && $values[$x]!=='')?$values[$x]:null;
  353. }
  354. /**
  355. * Return one row from the DB query (use if your doing LIMIT 1)
  356. * or are expecting/want only one row returned
  357. *
  358. * @param string $query The SQL Query
  359. * @param type $output OBJECT (fastest, default), ARRAY_A, ARRAY_N
  360. * @param string $y Row offset (0 for first, 1 for 2nd, etc)
  361. * @return type Returns type as defined in $output
  362. *
  363. */
  364. public function get_row($query=null,$output='',$y=0)
  365. {
  366. if($output == '') $output = $this->default_type;
  367. // Log how the function was called
  368. $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
  369. // If there is a query then perform it if not then use cached results..
  370. if ( $query )
  371. {
  372. $this->query($query);
  373. }
  374. // If the output is an object then return object using the row offset..
  375. if ( $output == OBJECT )
  376. {
  377. return $this->last_result[$y]?$this->last_result[$y]:null;
  378. }
  379. // If the output is an associative array then return row as such..
  380. elseif ( $output == ARRAY_A )
  381. {
  382. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  383. }
  384. // If the output is an numerical array then return row as such..
  385. elseif ( $output == ARRAY_N )
  386. {
  387. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  388. }
  389. // If invalid output type was specified..
  390. else
  391. {
  392. $this->print_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N");
  393. }
  394. }
  395. /**
  396. * Quote a value properly, check if its a function first though
  397. * For instance, passing NOW() will just return NOW. Just a simple
  398. * preg_match for string and a ( brace
  399. *
  400. * @param mixed $value Value to quote
  401. * @return mixed Returns a quoted or non quoted value
  402. *
  403. */
  404. public function quote($value)
  405. {
  406. if(is_numeric($value))
  407. {
  408. return $value;
  409. }
  410. return '\''.$value.'\'';
  411. }
  412. /**
  413. * Build a SELECT query, specifying the table, fields and extra conditions
  414. *
  415. * @param string $table Table to SELECT from
  416. * @param mixed $fields Array of fields to SELECT for, or comma delimited string of fields
  417. * @param string $cond Extra conditions (include the WHERE, LIMIT, etc)
  418. * @param int $limit Number of results to limit
  419. * @param type $type OBJECT, ARRAY_A, ARRAY_n
  420. * @return type Array of results
  421. *
  422. */
  423. public function quick_select($table, $fields, $cond='', $type='')
  424. {
  425. if($table == '') return false;
  426. if($type == '') $type = $this->default_type;
  427. $sql = 'SELECT ';
  428. if(is_array($fields))
  429. {
  430. $sql.= implode(',', $fields);
  431. }
  432. else
  433. {
  434. $sql .= $fields;
  435. }
  436. $sql .= ' FROM '.$table;
  437. if($cond != '')
  438. $sql .= ' '.$cond;
  439. return $this->get_results($sql, $type);
  440. }
  441. /**
  442. * Build a quick INSERT query. For simplistic INSERTs only,
  443. * all values except numeric and NOW() are put in quotes
  444. *
  445. * Values are NOT escaped
  446. *
  447. * @param string $table Table to insert into
  448. * @param array $fields Associative array [column] = value
  449. * @param string $flags Extra INSERT flags to add
  450. * @return bool Results
  451. *
  452. */
  453. public function quick_insert($table, $fields, $flags= '', $allowed_cols='')
  454. {
  455. if($table == '') return false;
  456. $sql = 'INSERT '. $flags .' INTO '.$table.' ';
  457. if(is_array($allowed_cols) && count($allowed_cols) > 0)
  458. {
  459. foreach($allowed_cols as $column)
  460. {
  461. $cols .= $column.',';
  462. $col_values .= $this->quote($fields[$column]).',';
  463. }
  464. }
  465. else
  466. {
  467. if(is_array($fields))
  468. {
  469. foreach($fields as $key=>$value)
  470. {
  471. // build both strings
  472. $cols .= $key.',';
  473. $col_values .= $this->quote($value).',';
  474. }
  475. }
  476. }
  477. $cols[strlen($cols)-1] = ' ';
  478. $col_values[strlen($col_values)-1] = ' ';
  479. $sql .= '('.$cols.') VALUES ('.$col_values.')';
  480. return $this->query($sql);
  481. }
  482. /**
  483. * Build a UPDATE SQL Query. All values except for
  484. * numeric and NOW() will be put in quotes
  485. *
  486. * Values are NOT escaped
  487. *
  488. * @param string $table Table to build update on
  489. * @param array $fields Associative array, with [column]=value
  490. * @param string $cond Extra conditions, without WHERE
  491. * @return bool Results
  492. *
  493. */
  494. public function quick_update($table, $fields, $cond='', $allowed_cols='')
  495. {
  496. if($table == '') return false;
  497. $sql = 'UPDATE '.$table.' SET ';
  498. /* If they passed an associative array of col=>value */
  499. if(is_array($fields))
  500. {
  501. /* Passed an array with the allowed columns */
  502. if(is_array($allowed_cols) && count($allowed_cols) > 0)
  503. {
  504. foreach($allowed_cols as $column)
  505. {
  506. $sql .= $column.'=';
  507. $sql .= $this->quote($fields[$column]).',';
  508. }
  509. }
  510. /* No specific columns, just process all the $fields */
  511. else
  512. {
  513. foreach($fields as $key=>$value)
  514. {
  515. $sql.= "$key=";
  516. $sql .= $this->quote($value).',';
  517. }
  518. }
  519. $sql = substr($sql, 0, strlen($sql)-1);
  520. }
  521. else
  522. {
  523. $sql .= $fields;
  524. }
  525. $cols[strlen($cols)-1] = ' ';
  526. $col_values[strlen($col_values)-1] = ' ';
  527. if($cond != '')
  528. $sql .= ' WHERE '.$cond;
  529. return $this->query($sql);
  530. }
  531. /**
  532. * Get the value of one column from a query
  533. *
  534. * @param string $query The SQL query
  535. * @param string $x Column to return
  536. * @return array Return's the results of that one column
  537. *
  538. */
  539. public function get_col($query=null,$x=0)
  540. {
  541. // If there is a query then perform it if not then use cached results..
  542. if ( $query )
  543. {
  544. $this->query($query);
  545. }
  546. // Extract the column values
  547. for ( $i=0; $i < count($this->last_result); $i++ )
  548. {
  549. $new_array[$i] = $this->get_var(null,$x,$i);
  550. }
  551. return $new_array;
  552. }
  553. /**
  554. * Returns the query as a set of results. Default returns OBJECT,
  555. * that is much faster than translating to ARRAY_A or ARRAY_N
  556. *
  557. * @param string $query SQL query
  558. * @param define $output OBJECT, ARRAY_A (associative array), ARRAY_N (numeric indexed). OBJECT is default and fastest
  559. * @return object Array of results, each array value being what $output is defined as
  560. *
  561. */
  562. public function get_results($query=null, $output = '')
  563. {
  564. if($output == '') $output = $this->default_type;
  565. // Log how the function was called
  566. $this->func_call = "\$db->get_results(\"$query\", $output)";
  567. // If there is a query then perform it if not then use cached results..
  568. if ( $query )
  569. {
  570. $this->query($query);
  571. }
  572. // Send back array of objects. Each row is an object
  573. if ( $output == OBJECT )
  574. {
  575. return $this->last_result;
  576. }
  577. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  578. {
  579. if ( $this->last_result )
  580. {
  581. $i=0;
  582. foreach( $this->last_result as $row )
  583. {
  584. $new_array[$i] = get_object_vars($row);
  585. if ( $output == ARRAY_N )
  586. {
  587. $new_array[$i] = array_values($new_array[$i]);
  588. }
  589. $i++;
  590. }
  591. return $new_array;
  592. }
  593. else
  594. {
  595. return null;
  596. }
  597. }
  598. }
  599. /**
  600. * Get metadata regarding a column, about a column in the last query
  601. *
  602. * @param string $info_type Column information type to get
  603. * @param int $col_offset Column number, -1 returns all columns
  604. * @return array Column information
  605. *
  606. */
  607. public function get_col_info($info_type='name',$col_offset=-1)
  608. {
  609. if ($this->col_info )
  610. {
  611. if ( $col_offset == -1 )
  612. {
  613. $i=0;
  614. foreach($this->col_info as $col )
  615. {
  616. $new_array[$i] = $col->{$info_type};
  617. $i++;
  618. }
  619. return $new_array;
  620. }
  621. else
  622. {
  623. return $this->col_info[$col_offset]->{$info_type};
  624. }
  625. }
  626. return false;
  627. }
  628. /**
  629. * Store a results in the cache for a certain query
  630. *
  631. * @param string $query SQL query to store
  632. * @param bool $is_insert If it's an INSERT or not
  633. * @return bool Success
  634. *
  635. */
  636. public function store_cache($query,$is_insert)
  637. {
  638. if($this->cache_queries === false || $is_insert)
  639. return false;
  640. $result_cache = array('col_info' => $this->col_info,
  641. 'last_result' => $this->last_result,
  642. 'num_rows' => $this->num_rows,
  643. 'return_value' => $this->num_rows);
  644. if($this->cache_type == 'memcache')
  645. {
  646. }
  647. elseif($this->cache_type == 'apc')
  648. {
  649. apc_add(md5($query), $result_cache, $this->cache_timeout);
  650. }
  651. else
  652. {
  653. $cache_file = $this->cache_dir.'/'.md5($query);
  654. if ( ! is_dir($this->cache_dir) )
  655. {
  656. $this->register_error("Could not open cache dir: $this->cache_dir");
  657. return false;
  658. }
  659. error_log ( serialize($result_cache), 3, $cache_file);
  660. }
  661. return true;
  662. }
  663. /**
  664. * Get the cached results for a query. This is called more internally
  665. *
  666. * @param string $query SQL query to return results for
  667. * @return mixed Returns the unserialized results
  668. *
  669. */
  670. public function get_cache($query)
  671. {
  672. if($this->cache_queries === false || $is_insert)
  673. return false;
  674. # Check if we want to us memcache, and whether it's available
  675. if($this->cache_type == 'memcache')
  676. {
  677. }
  678. elseif($this->cache_type == 'apc')
  679. {
  680. $result_cache = apc_fetch(md5($query), $ret);
  681. }
  682. # Use type "file" for any other cache_type
  683. else
  684. {
  685. // The would be cache file for this query
  686. $cache_file = $this->cache_dir.'/'.md5($query);
  687. // Try to get previously cached version
  688. if (file_exists($cache_file) )
  689. {
  690. // Only use this cache file if less than 'cache_timeout' (hours)
  691. if (time() - filemtime($cache_file) > $this->cache_timeout )
  692. {
  693. unlink($cache_file);
  694. }
  695. else
  696. {
  697. $result_cache = unserialize(file_get_contents($cache_file));
  698. }
  699. }
  700. }
  701. $this->from_cache = true;
  702. $this->col_info = $result_cache['col_info'];
  703. $this->last_result = $result_cache['last_result'];
  704. $this->num_rows = $result_cache['num_rows'];
  705. $this->trace || $this->debug_all ? $this->debug() : null ;
  706. return $result_cache['return_value'];
  707. }
  708. /**
  709. * Show values of any variable type "nicely"
  710. *
  711. * @param mixed $mixed Variable to show
  712. * @param bool $return Return the results or show on screen
  713. * @return mixed This is the return value description
  714. *
  715. */
  716. public function vardump($mixed='', $return=false)
  717. {
  718. // Start outup buffering
  719. ob_start();
  720. echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
  721. echo "<pre><font face=arial>";
  722. if ( ! $this->vardump_called )
  723. {
  724. echo "<font color=800080><b>ezDB</b> (v".ezDB_VERSION.") <b>Variable Dump..</b></font>\n\n";
  725. }
  726. $var_type = gettype ($mixed);
  727. print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
  728. echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
  729. echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  730. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  731. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  732. echo "</font></pre></font></blockquote></td></tr></table>".$this->donation();
  733. echo "\n<hr size=1 noshade color=dddddd>";
  734. // Stop output buffering and capture debug HTML
  735. $html = ob_get_contents();
  736. ob_end_clean();
  737. // Only echo output if it is turned on
  738. if ( $this->debug_echo_is_on || $return == false)
  739. {
  740. echo $html;
  741. }
  742. $this->vardump_called = true;
  743. return $html;
  744. }
  745. /**
  746. * Show values of any variable type "nicely"
  747. *
  748. * @param mixed $mixed Variable to show
  749. * @param bool $return Return the results or show on screen
  750. * @return mixed This is the return value description
  751. *
  752. */
  753. public function dumpvar($mixed, $return=false)
  754. {
  755. $this->vardump($mixed, $return);
  756. }
  757. /**
  758. * Displays the last query string that was sent to the database & a
  759. * table listing results (if there were any).
  760. * (abstracted into a seperate file to save server overhead).
  761. *
  762. * @param bool $return Return the results, or display right away
  763. * @return string The debug table is $return = true
  764. *
  765. */
  766. public function debug($return=false)
  767. {
  768. // Start outup buffering
  769. ob_start();
  770. echo "<blockquote>";
  771. // Only show ezDB credits once..
  772. if ( ! $this->debug_called )
  773. {
  774. echo "<font color=800080 face=arial size=2><b>ezDB</b> (v".ezDB_VERSION.") <b>Debug..</b></font><p>\n";
  775. }
  776. if ( $this->error )
  777. {
  778. echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->error ($this->errno)</b></font>]<p>";
  779. }
  780. if ( $this->from_disk_cache )
  781. {
  782. echo "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>";
  783. }
  784. echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
  785. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  786. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  787. echo "<blockquote>";
  788. if ( $this->col_info )
  789. {
  790. // =====================================================
  791. // Results top rows
  792. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  793. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  794. for ( $i=0; $i < count($this->col_info); $i++ )
  795. {
  796. 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}</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>{$this->col_info[$i]->name}</span></td>";
  797. }
  798. echo "</tr>";
  799. // ======================================================
  800. // print main results
  801. if ( $this->last_result )
  802. {
  803. $i=0;
  804. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  805. {
  806. $i++;
  807. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  808. foreach ( $one_row as $item )
  809. {
  810. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  811. }
  812. echo "</tr>";
  813. }
  814. } // if last result
  815. else
  816. {
  817. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  818. }
  819. echo "</table>";
  820. } // if col_info
  821. else
  822. {
  823. echo "<font face=arial size=2>No Results</font>";
  824. }
  825. echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
  826. // Stop output buffering and capture debug HTML
  827. $html = ob_get_contents();
  828. ob_end_clean();
  829. // Only echo output if it is turned on
  830. if ( $this->debug_echo_is_on || $return == false )
  831. {
  832. echo $html;
  833. }
  834. $this->debug_called = true;
  835. return $html;
  836. }
  837. /**********************************************************************
  838. * Naughty little function to ask for some remuniration!
  839. */
  840. public function donation()
  841. {
  842. return "<font size=1 face=arial color=000000>If ezDB has helped <a href=\"https://www.paypal.com/xclick/business=justin%40justinvincent.com&item_name=ezDB&no_note=1&tax=0\" style=\"color: 0000CC;\">make a donation!?</a> &nbsp;&nbsp;<!--[ go on! you know you want to! ]--></font>";
  843. }
  844. }
  845. ?>