PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/forum/db/mysql4.php

https://bitbucket.org/matthewsomerville/publicwhip-v1
PHP | 340 lines | 255 code | 49 blank | 36 comment | 42 complexity | f728a69be135732e1c19fa453b54cf09 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause
  1. <?php
  2. /***************************************************************************
  3. * mysql4.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : supportphpbb.com
  8. *
  9. * $Id: mysql4.php,v 1.2 2007/05/20 07:21:34 frabcus Exp $
  10. *
  11. ***************************************************************************/
  12. /***************************************************************************
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. ***************************************************************************/
  20. if(!defined("SQL_LAYER"))
  21. {
  22. define("SQL_LAYER","mysql4");
  23. class sql_db
  24. {
  25. var $db_connect_id;
  26. var $query_result;
  27. var $row = array();
  28. var $rowset = array();
  29. var $num_queries = 0;
  30. var $in_transaction = 0;
  31. //
  32. // Constructor
  33. //
  34. function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
  35. {
  36. $this->persistency = $persistency;
  37. $this->user = $sqluser;
  38. $this->password = $sqlpassword;
  39. $this->server = $sqlserver;
  40. $this->dbname = $database;
  41. $this->db_connect_id = ($this->persistency) ? mysql_pconnect($this->server, $this->user, $this->password) : mysql_connect($this->server, $this->user, $this->password);
  42. if( $this->db_connect_id )
  43. {
  44. if( $database != "" )
  45. {
  46. $this->dbname = $database;
  47. $dbselect = mysql_select_db($this->dbname);
  48. if( !$dbselect )
  49. {
  50. mysql_close($this->db_connect_id);
  51. $this->db_connect_id = $dbselect;
  52. }
  53. }
  54. return $this->db_connect_id;
  55. }
  56. else
  57. {
  58. return false;
  59. }
  60. }
  61. //
  62. // Other base methods
  63. //
  64. function sql_close()
  65. {
  66. if( $this->db_connect_id )
  67. {
  68. //
  69. // Commit any remaining transactions
  70. //
  71. if( $this->in_transaction )
  72. {
  73. mysql_query("COMMIT", $this->db_connect_id);
  74. }
  75. return mysql_close($this->db_connect_id);
  76. }
  77. else
  78. {
  79. return false;
  80. }
  81. }
  82. //
  83. // Base query method
  84. //
  85. function sql_query($query = "", $transaction = FALSE)
  86. {
  87. //
  88. // Remove any pre-existing queries
  89. //
  90. unset($this->query_result);
  91. if( $query != "" )
  92. {
  93. $this->num_queries++;
  94. if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
  95. {
  96. $result = mysql_query("BEGIN", $this->db_connect_id);
  97. if(!$result)
  98. {
  99. return false;
  100. }
  101. $this->in_transaction = TRUE;
  102. }
  103. $this->query_result = mysql_query($query, $this->db_connect_id);
  104. }
  105. else
  106. {
  107. if( $transaction == END_TRANSACTION && $this->in_transaction )
  108. {
  109. $result = mysql_query("COMMIT", $this->db_connect_id);
  110. }
  111. }
  112. if( $this->query_result )
  113. {
  114. unset($this->row[$this->query_result]);
  115. unset($this->rowset[$this->query_result]);
  116. if( $transaction == END_TRANSACTION && $this->in_transaction )
  117. {
  118. $this->in_transaction = FALSE;
  119. if ( !mysql_query("COMMIT", $this->db_connect_id) )
  120. {
  121. mysql_query("ROLLBACK", $this->db_connect_id);
  122. return false;
  123. }
  124. }
  125. return $this->query_result;
  126. }
  127. else
  128. {
  129. if( $this->in_transaction )
  130. {
  131. mysql_query("ROLLBACK", $this->db_connect_id);
  132. $this->in_transaction = FALSE;
  133. }
  134. return false;
  135. }
  136. }
  137. //
  138. // Other query methods
  139. //
  140. function sql_numrows($query_id = 0)
  141. {
  142. if( !$query_id )
  143. {
  144. $query_id = $this->query_result;
  145. }
  146. return ( $query_id ) ? mysql_num_rows($query_id) : false;
  147. }
  148. function sql_affectedrows()
  149. {
  150. return ( $this->db_connect_id ) ? mysql_affected_rows($this->db_connect_id) : false;
  151. }
  152. function sql_numfields($query_id = 0)
  153. {
  154. if( !$query_id )
  155. {
  156. $query_id = $this->query_result;
  157. }
  158. return ( $query_id ) ? mysql_num_fields($query_id) : false;
  159. }
  160. function sql_fieldname($offset, $query_id = 0)
  161. {
  162. if( !$query_id )
  163. {
  164. $query_id = $this->query_result;
  165. }
  166. return ( $query_id ) ? mysql_field_name($query_id, $offset) : false;
  167. }
  168. function sql_fieldtype($offset, $query_id = 0)
  169. {
  170. if( !$query_id )
  171. {
  172. $query_id = $this->query_result;
  173. }
  174. return ( $query_id ) ? mysql_field_type($query_id, $offset) : false;
  175. }
  176. function sql_fetchrow($query_id = 0)
  177. {
  178. if( !$query_id )
  179. {
  180. $query_id = $this->query_result;
  181. }
  182. if( $query_id )
  183. {
  184. $this->row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  185. return $this->row[$query_id];
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. function sql_fetchrowset($query_id = 0)
  193. {
  194. if( !$query_id )
  195. {
  196. $query_id = $this->query_result;
  197. }
  198. if( $query_id )
  199. {
  200. unset($this->rowset[$query_id]);
  201. unset($this->row[$query_id]);
  202. while($this->rowset[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC))
  203. {
  204. $result[] = $this->rowset[$query_id];
  205. }
  206. return $result;
  207. }
  208. else
  209. {
  210. return false;
  211. }
  212. }
  213. function sql_fetchfield($field, $rownum = -1, $query_id = 0)
  214. {
  215. if( !$query_id )
  216. {
  217. $query_id = $this->query_result;
  218. }
  219. if( $query_id )
  220. {
  221. if( $rownum > -1 )
  222. {
  223. $result = mysql_result($query_id, $rownum, $field);
  224. }
  225. else
  226. {
  227. if( empty($this->row[$query_id]) && empty($this->rowset[$query_id]) )
  228. {
  229. if( $this->sql_fetchrow() )
  230. {
  231. $result = $this->row[$query_id][$field];
  232. }
  233. }
  234. else
  235. {
  236. if( $this->rowset[$query_id] )
  237. {
  238. $result = $this->rowset[$query_id][0][$field];
  239. }
  240. else if( $this->row[$query_id] )
  241. {
  242. $result = $this->row[$query_id][$field];
  243. }
  244. }
  245. }
  246. return $result;
  247. }
  248. else
  249. {
  250. return false;
  251. }
  252. }
  253. function sql_rowseek($rownum, $query_id = 0)
  254. {
  255. if( !$query_id )
  256. {
  257. $query_id = $this->query_result;
  258. }
  259. return ( $query_id ) ? mysql_data_seek($query_id, $rownum) : false;
  260. }
  261. function sql_nextid()
  262. {
  263. return ( $this->db_connect_id ) ? mysql_insert_id($this->db_connect_id) : false;
  264. }
  265. function sql_freeresult($query_id = 0)
  266. {
  267. if( !$query_id )
  268. {
  269. $query_id = $this->query_result;
  270. }
  271. if ( $query_id )
  272. {
  273. unset($this->row[$query_id]);
  274. unset($this->rowset[$query_id]);
  275. mysql_free_result($query_id);
  276. return true;
  277. }
  278. else
  279. {
  280. return false;
  281. }
  282. }
  283. function sql_error()
  284. {
  285. $result['message'] = mysql_error($this->db_connect_id);
  286. $result['code'] = mysql_errno($this->db_connect_id);
  287. return $result;
  288. }
  289. } // class sql_db
  290. } // if ... define
  291. ?>