/classes/Database/Query/mysql.php

https://github.com/MythTV/mythweb · PHP · 191 lines · 77 code · 17 blank · 97 comment · 11 complexity · 0d5ae73506be43100efa15f1bbc64067 MD5 · raw file

  1. <?php
  2. /**
  3. * This file was originally written by Chris Petersen for several different open
  4. * source projects. It is distrubuted under the GNU General Public License.
  5. * I (Chris Petersen) have also granted a special LGPL license for this code to
  6. * several companies I do work for on the condition that these companies will
  7. * release any changes to this back to me and the open source community as GPL,
  8. * thus continuing to improve the open source version of the library. If you
  9. * would like to inquire about the status of this arrangement, please contact
  10. * me personally.
  11. *
  12. * ---
  13. *
  14. * Query handler for MySQL
  15. *
  16. * @copyright Silicon Mechanics
  17. * @license GPL
  18. *
  19. * @package MythWeb
  20. * @subpackage Database
  21. *
  22. * @uses Database.php
  23. * @uses Database_mysql.php
  24. * @uses Database_Query.php
  25. *
  26. **/
  27. /**
  28. * The basic MySQL database query type.
  29. **/
  30. class Database_Query_mysql extends Database_Query {
  31. /**
  32. * Executes the query that was previously passed to the constructor.
  33. *
  34. * @param mixed $arg Query arguments to escape and insert at ? placeholders in $query
  35. * @param mixed ... Additional arguments
  36. **/
  37. function execute() {
  38. // Load the function arguments, minus the query itself, which we already extracted
  39. $args = func_get_args();
  40. // Split out sub-arrays, etc..
  41. $args = Database::smart_args($args);
  42. // Were enough arguments passed in?
  43. if (count($args) != $this->num_args_needed)
  44. trigger_error('Database_Query called with '.count($args)." arguments, but requires $this->num_args_needed.", E_USER_ERROR);
  45. // Finish any previous statements
  46. $this->finish();
  47. // Replace in the arguments
  48. $this->last_query = '';
  49. foreach ($this->query as $part) {
  50. $this->last_query .= $part;
  51. if (count($args)) {
  52. $arg = array_shift($args);
  53. $this->last_query .= is_null($arg)
  54. ? 'NULL'
  55. : "'".mysqli_real_escape_string($arg)."'";
  56. }
  57. }
  58. // Perform the query
  59. // If we don't have a valid connection, fataly error out.
  60. if ($this->dbh === false) {
  61. $this->db->error();
  62. trigger_error($this->db->error, E_USER_ERROR);
  63. }
  64. $this->sh = mysqli_query($this->last_query, $this->dbh);
  65. // Cache these
  66. if (is_bool($this->sh)) {
  67. $this->insert_id = mysqli_insert_id($this->dbh);
  68. $this->affected_rows = mysqli_affected_rows($this->dbh);
  69. }
  70. else {
  71. $this->num_rows = mysqli_num_rows($this->sh);
  72. }
  73. if ($this->sh === false) {
  74. if ($this->db->fatal_errors)
  75. trigger_error('SQL Error: '.mysqli_error($this->dbh).' [#'.mysqli_errno($this->dbh).']', E_USER_ERROR);
  76. else
  77. $this->db->error();
  78. }
  79. }
  80. /**
  81. * The following routines basically replicate the mysql functions built into
  82. * php. The only difference is that the resource handle gets passed-in
  83. * automatically. eg.
  84. *
  85. * mysqli_fetch_row($result); -> $sh->fetch_row();
  86. * mysqli_affected_rows($dbh); -> $sh->affected_rows();
  87. **/
  88. /**
  89. * Fetch a single column
  90. * @return mixed
  91. **/
  92. function fetch_col() {
  93. list($return) = mysqli_fetch_row($this->sh);
  94. return $return;
  95. }
  96. function fetch_cols() {
  97. $return = array();
  98. while ($col = $this->fetch_col())
  99. $return[] = $col;
  100. return $return;
  101. }
  102. /**
  103. * Fetch a single row
  104. *
  105. * @link http://www.php.net/manual/en/function.mysql-fetch-row.php
  106. * @return array
  107. **/
  108. function fetch_row() {
  109. return mysqli_fetch_row($this->sh);
  110. }
  111. /**
  112. * Fetch a single assoc row
  113. *
  114. * @link http://www.php.net/manual/en/function.mysql-fetch-assoc.php
  115. * @return assoc
  116. **/
  117. function fetch_assoc() {
  118. return mysqli_fetch_assoc($this->sh);
  119. }
  120. /**
  121. * Fetch a single row as an array containing both numeric and assoc fields
  122. *
  123. * @link http://www.php.net/manual/en/function.mysql-fetch-array.php
  124. * @return assoc
  125. **/
  126. function fetch_array($result_type=MYSQLI_BOTH) {
  127. return mysqli_fetch_array($this->sh, $result_type);
  128. }
  129. /**
  130. * Fetch a single row as an object
  131. *
  132. * @link http://www.php.net/manual/en/function.mysql-fetch-object.php
  133. * @return object
  134. **/
  135. function fetch_object() {
  136. return mysqli_fetch_object($this->sh);
  137. }
  138. /**
  139. * @link http://www.php.net/manual/en/function.mysql-data-seek.php
  140. * @return bool
  141. **/
  142. function data_seek($row_number) {
  143. return mysqli_data_seek($this->sh, $row_number);
  144. }
  145. /**
  146. * @link http://www.php.net/manual/en/function.mysql-num-rows.php
  147. * @return int
  148. **/
  149. function num_rows() {
  150. return $this->num_rows;
  151. }
  152. /**
  153. * @link http://www.php.net/manual/en/function.mysql-data-seek.php
  154. * @return int
  155. **/
  156. function affected_rows() {
  157. return $this->affected_rows;
  158. }
  159. /**
  160. * @link http://www.php.net/manual/en/function.mysql-insert-id.php
  161. * @return int
  162. **/
  163. function insert_id() {
  164. return $this->insert_id;
  165. }
  166. /**
  167. * For anal people like me who like to free up memory manually
  168. **/
  169. function finish() {
  170. if ($this->sh && is_resource($this->sh))
  171. mysqli_free_result($this->sh);
  172. unset($this->sh);
  173. }
  174. }