Просмотр файла includes/db/mysql4.php

Размер файла: 5.83Kb
  1. <?php
  2. /***************************************************************************
  3. * mides.ru
  4. * -------------------
  5. ***************************************************************************/
  6. if ( !defined('IN_PHPBB') )
  7. {
  8. die("ERROR!!! THIS FILE PROTECTED. IF YOU SAW THIS REPORT, MEANS HACKERS HERE IS NOTHING TO DO ");
  9. }
  10.  
  11. if(!defined("SQL_LAYER"))
  12. {
  13.  
  14. define("SQL_LAYER","mysql4");
  15.  
  16. class sql_db
  17. {
  18.  
  19. var $db_connect_id;
  20. var $query_result;
  21. var $row = array();
  22. var $rowset = array();
  23. var $num_queries = 0;
  24. var $in_transaction = 0;
  25.  
  26. //
  27. // Constructor
  28. //
  29. function sql_db($sqlserver, $sqluser, $sqlpassword, $database, $persistency = true)
  30. {
  31. $this->persistency = $persistency;
  32. $this->user = $sqluser;
  33. $this->password = $sqlpassword;
  34. $this->server = $sqlserver;
  35. $this->dbname = $database;
  36.  
  37. $this->db_connect_id = ($this->persistency) ? mysql_pconnect($this->server, $this->user, $this->password) : mysql_connect($this->server, $this->user, $this->password);
  38.  
  39. if( $this->db_connect_id )
  40. {
  41. if( $database != "" )
  42. {
  43. $this->dbname = $database;
  44. $dbselect = mysql_select_db($this->dbname);
  45.  
  46. if( !$dbselect )
  47. {
  48. mysql_close($this->db_connect_id);
  49. $this->db_connect_id = $dbselect;
  50. }
  51. }
  52.  
  53. return $this->db_connect_id;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  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.  
  76. return mysql_close($this->db_connect_id);
  77. }
  78. else
  79. {
  80. return false;
  81. }
  82. }
  83.  
  84. //
  85. // Base query method
  86. //
  87. function sql_query($query = "", $transaction = FALSE)
  88. {
  89. //
  90. // Remove any pre-existing queries
  91. //
  92. unset($this->query_result);
  93.  
  94. if( $query != "" )
  95. {
  96. $this->num_queries++;
  97. if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
  98. {
  99. $result = mysql_query("BEGIN", $this->db_connect_id);
  100. if(!$result)
  101. {
  102. return false;
  103. }
  104. $this->in_transaction = TRUE;
  105. }
  106.  
  107. $this->query_result = mysql_query($query, $this->db_connect_id);
  108. }
  109. else
  110. {
  111. if( $transaction == END_TRANSACTION && $this->in_transaction )
  112. {
  113. $result = mysql_query("COMMIT", $this->db_connect_id);
  114. }
  115. }
  116.  
  117. if( $this->query_result )
  118. {
  119. unset($this->row[$this->query_result]);
  120. unset($this->rowset[$this->query_result]);
  121.  
  122. if( $transaction == END_TRANSACTION && $this->in_transaction )
  123. {
  124. $this->in_transaction = FALSE;
  125.  
  126. if ( !mysql_query("COMMIT", $this->db_connect_id) )
  127. {
  128. mysql_query("ROLLBACK", $this->db_connect_id);
  129. return false;
  130. }
  131. }
  132. return $this->query_result;
  133. }
  134. else
  135. {
  136. if( $this->in_transaction )
  137. {
  138. mysql_query("ROLLBACK", $this->db_connect_id);
  139. $this->in_transaction = FALSE;
  140. }
  141. return false;
  142. }
  143. }
  144.  
  145. //
  146. // Other query methods
  147. //
  148. function sql_numrows($query_id = 0)
  149. {
  150. if( !$query_id )
  151. {
  152. $query_id = $this->query_result;
  153. }
  154.  
  155. return ( $query_id ) ? mysql_num_rows($query_id) : false;
  156. }
  157.  
  158. function sql_affectedrows()
  159. {
  160. return ( $this->db_connect_id ) ? mysql_affected_rows($this->db_connect_id) : false;
  161. }
  162.  
  163. function sql_numfields($query_id = 0)
  164. {
  165. if( !$query_id )
  166. {
  167. $query_id = $this->query_result;
  168. }
  169.  
  170. return ( $query_id ) ? mysql_num_fields($query_id) : false;
  171. }
  172.  
  173. function sql_fieldname($offset, $query_id = 0)
  174. {
  175. if( !$query_id )
  176. {
  177. $query_id = $this->query_result;
  178. }
  179.  
  180. return ( $query_id ) ? mysql_field_name($query_id, $offset) : false;
  181. }
  182.  
  183. function sql_fieldtype($offset, $query_id = 0)
  184. {
  185. if( !$query_id )
  186. {
  187. $query_id = $this->query_result;
  188. }
  189.  
  190. return ( $query_id ) ? mysql_field_type($query_id, $offset) : false;
  191. }
  192.  
  193. function sql_fetchrow($query_id = 0)
  194. {
  195. if( !$query_id )
  196. {
  197. $query_id = $this->query_result;
  198. }
  199.  
  200. if( $query_id )
  201. {
  202. $this->row[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC);
  203. return $this->row[$query_id];
  204. }
  205. else
  206. {
  207. return false;
  208. }
  209. }
  210.  
  211. function sql_fetchrowset($query_id = 0)
  212. {
  213. if( !$query_id )
  214. {
  215. $query_id = $this->query_result;
  216. }
  217.  
  218. if( $query_id )
  219. {
  220. unset($this->rowset[$query_id]);
  221. unset($this->row[$query_id]);
  222.  
  223. while($this->rowset[$query_id] = mysql_fetch_array($query_id, MYSQL_ASSOC))
  224. {
  225. $result[] = $this->rowset[$query_id];
  226. }
  227.  
  228. return $result;
  229. }
  230. else
  231. {
  232. return false;
  233. }
  234. }
  235.  
  236. function sql_fetchfield($field, $rownum = -1, $query_id = 0)
  237. {
  238. if( !$query_id )
  239. {
  240. $query_id = $this->query_result;
  241. }
  242.  
  243. if( $query_id )
  244. {
  245. if( $rownum > -1 )
  246. {
  247. $result = mysql_result($query_id, $rownum, $field);
  248. }
  249. else
  250. {
  251. if( empty($this->row[$query_id]) && empty($this->rowset[$query_id]) )
  252. {
  253. if( $this->sql_fetchrow() )
  254. {
  255. $result = $this->row[$query_id][$field];
  256. }
  257. }
  258. else
  259. {
  260. if( $this->rowset[$query_id] )
  261. {
  262. $result = $this->rowset[$query_id][0][$field];
  263. }
  264. else if( $this->row[$query_id] )
  265. {
  266. $result = $this->row[$query_id][$field];
  267. }
  268. }
  269. }
  270.  
  271. return $result;
  272. }
  273. else
  274. {
  275. return false;
  276. }
  277. }
  278.  
  279. function sql_rowseek($rownum, $query_id = 0)
  280. {
  281. if( !$query_id )
  282. {
  283. $query_id = $this->query_result;
  284. }
  285.  
  286. return ( $query_id ) ? mysql_data_seek($query_id, $rownum) : false;
  287. }
  288.  
  289. function sql_nextid()
  290. {
  291. return ( $this->db_connect_id ) ? mysql_insert_id($this->db_connect_id) : false;
  292. }
  293.  
  294. function sql_freeresult($query_id = 0)
  295. {
  296. if( !$query_id )
  297. {
  298. $query_id = $this->query_result;
  299. }
  300.  
  301. if ( $query_id )
  302. {
  303. unset($this->row[$query_id]);
  304. unset($this->rowset[$query_id]);
  305.  
  306. mysql_free_result($query_id);
  307.  
  308. return true;
  309. }
  310. else
  311. {
  312. return false;
  313. }
  314. }
  315.  
  316. function sql_error()
  317. {
  318. $result['message'] = mysql_error($this->db_connect_id);
  319. $result['code'] = mysql_errno($this->db_connect_id);
  320.  
  321. return $result;
  322. }
  323.  
  324. } // class sql_db
  325.  
  326. } // if ... define
  327.  
  328. ?>