/lib/MySQL/Improved.php

https://github.com/maniator/SmallFry · PHP · 236 lines · 156 code · 27 blank · 53 comment · 34 complexity · 211f1ccfe844efba4ab21d2d111d0b16 MD5 · raw file

  1. <?php
  2. namespace SmallFry\lib;
  3. /**
  4. * Description of MySQL
  5. *
  6. * @author nlubin
  7. */
  8. class MySQL_Improved extends \mysqli implements MySQL_Interface {
  9. private $_result;
  10. private $_last_query = null;
  11. private $_db_name;
  12. /**
  13. * Start a mysqli connection
  14. * @param string $server
  15. * @param string $username
  16. * @param string $password
  17. * @param string $dbname
  18. */
  19. function __construct($server, $username, $password, $dbname, $debug = false) {
  20. parent::__construct($server, $username, $password, $dbname);
  21. $this->_db_name = $dbname;
  22. if (mysqli_connect_errno()) {
  23. printf("Connect failed: %s\n", mysqli_connect_error());
  24. exit();
  25. }
  26. $this->debug = $debug;
  27. }
  28. function getSchemaName(){
  29. return $this->_db_name;
  30. }
  31. /**
  32. * Run a mysql query
  33. * @param string $query
  34. * @return mysqli_result
  35. */
  36. function run_query($query) {
  37. if($this->debug) DebugLogger::displayLog($query, true, true);
  38. $this->_last_query = $query;
  39. $this->_result = $this->query($query);
  40. if($this->_result) {
  41. if($this->debug) DebugLogger::displayLog(sprintf("Number of rows: %d ", $this->_result->num_rows), false, true);
  42. return $this->_result;
  43. }
  44. else {
  45. return false;
  46. }
  47. }
  48. /**
  49. *
  50. * @param mysqli_result $result
  51. * @return mixed
  52. */
  53. function get_row($result = null, $fetchby = MYSQLI_BOTH) {
  54. if($result == null){
  55. $result = $this->_result;
  56. }
  57. if($result instanceof \mysqli_stmt){
  58. return $this->get_bound_row($result, $fetchby);
  59. }
  60. else {
  61. switch($fetchby){
  62. case MYSQLI_ASSOC: {
  63. if($result && $row = $result->fetch_assoc()) {
  64. return $row;
  65. }
  66. break;
  67. }
  68. case MYSQLI_NUM: {
  69. if($result && $row = $result->fetch_array(MYSQLI_NUM)) {
  70. return $row;
  71. }
  72. break;
  73. }
  74. case MYSQLI_BOTH:
  75. default: {
  76. if($result && $row = $result->fetch_array()) {
  77. return $row;
  78. }
  79. break;
  80. }
  81. }
  82. if($result != null) {
  83. $result->free();
  84. }
  85. return false;
  86. }
  87. }
  88. function get_bound_row(&$result, $fetchby) {
  89. if($result && $row = $result->fetch_assoc()) {
  90. return $row;
  91. }
  92. return false;
  93. }
  94. /**
  95. *
  96. * @param mysqli_result $result
  97. * @return mixed
  98. */
  99. function get_num_rows($result = null) {
  100. if($result == null){
  101. $result = $this->_result;
  102. }
  103. if(!$result) return false;
  104. return $result->num_rows;
  105. }
  106. /**
  107. *
  108. * @return int
  109. */
  110. function get_last_insert_id(){
  111. return $this->insert_id;
  112. }
  113. /**
  114. *
  115. * @param mysqli_result $result
  116. * @return bool
  117. */
  118. function close_result($result = null) {
  119. if($result == null){
  120. $result = $this->_result;
  121. }
  122. if(!$result) return false;
  123. return $result->close();
  124. }
  125. /**
  126. *
  127. * @return string
  128. */
  129. function get_last_error($show_error = true) {
  130. $error = $this->error;
  131. if($show_error){
  132. $error .= "<pre>\nQuery:\n" . $this->_last_query . "</pre>";
  133. }
  134. return $error;
  135. }
  136. function start_transaction(){
  137. $this->autocommit(FALSE);
  138. /* @var $query mysqli_result */
  139. $query = $this->query("START TRANSACTION");
  140. }
  141. /**
  142. *
  143. * @return bool
  144. */
  145. function rollback(){
  146. $rb = parent::rollback();
  147. return $rb;
  148. }
  149. /**
  150. *
  151. * @return bool
  152. */
  153. function commit(){
  154. $cm = parent::commit();
  155. return $cm;
  156. }
  157. public function prepare($query)
  158. {
  159. if($this->debug) DebugLogger::displayLog($query, true, true);
  160. $stmt = new mysqliStmt_Extended($this, $query, $this->debug);
  161. return $stmt;
  162. }
  163. }
  164. class mysqliStmt_Extended extends \mysqli_stmt implements stmt_extended
  165. {
  166. protected $varsBound = false;
  167. protected $results;
  168. protected $debug = false;
  169. public function __construct($link, $query, $debug = false)
  170. {
  171. parent::__construct($link, $query);
  172. $this->debug = $debug;
  173. }
  174. public function log_bind_param($params) {
  175. $prepare = array();
  176. foreach($params as $key=>$option) {
  177. $prepare[] = &$params[$key]; //set as by reference
  178. }
  179. if($this->debug) DebugLogger::displayLog("bind_param " . print_r($prepare, true), true, true);
  180. call_user_func_array(array($this, "bind_param"), $prepare);
  181. }
  182. public function fetch_assoc()
  183. {
  184. // checks to see if the variables have been bound, this is so that when
  185. // using a while ($row = $this->stmt->fetch_assoc()) loop the following
  186. // code is only executed the first time
  187. if (!$this->varsBound) {
  188. $meta = $this->result_metadata();
  189. if($meta) {
  190. while ($column = $meta->fetch_field()) {
  191. // this is to stop a syntax error if a column name has a space in
  192. $columnName = str_replace(' ', '_', $column->name);
  193. $bindVarArray[] = &$this->results[$columnName];
  194. }
  195. call_user_func_array(array($this, 'bind_result'), $bindVarArray);
  196. }
  197. $this->varsBound = true;
  198. }
  199. $fetch = $this->fetch();
  200. if ($fetch != null) {
  201. foreach ($this->results as $k => $v) {
  202. $results[$k] = $v;
  203. }
  204. return $results;
  205. } else {
  206. return null;
  207. }
  208. }
  209. }