/web/public_php/admin/functions_mysqli.php

https://github.com/ryzom/ryzomcore · PHP · 302 lines · 235 code · 23 blank · 44 comment · 31 complexity · 741522813bae8f41131213750ca60d43 MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. * mysql.php
  4. * -------------------
  5. * begin : Saturday, Feb 13, 2001
  6. * copyright : (C) 2001 The phpBB Group
  7. * email : support@phpbb.com
  8. *
  9. * $Id: functions_mysql.php,v 1.2 2006/07/06 15:17:22 powles 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. /***************************************************************************
  21. *
  22. * NOTE: this is striped down version from phpBB mysql.php file
  23. * modified to work with mysqli
  24. *
  25. ***************************************************************************/
  26. if(!defined("SQL_LAYER"))
  27. {
  28. define("SQL_LAYER","mysqli");
  29. class sql_db
  30. {
  31. var $db_connect_id;
  32. var $query_result;
  33. var $num_queries = 0;
  34. //
  35. // Constructor
  36. //
  37. function __construct($sqlserver, $sqlport, $sqluser, $sqlpassword, $database, $persistency = true)
  38. {
  39. $this->persistency = $persistency;
  40. $this->user = $sqluser;
  41. $this->password = $sqlpassword;
  42. $this->server = $sqlserver;
  43. $this->port = $sqlport;
  44. $this->dbname = $database;
  45. if($this->persistency)
  46. {
  47. $this->server = 'p:'.$this->server;
  48. }
  49. $this->db_connect_id = mysqli_connect($this->server, $this->user, $this->password, NULL, $this->port);
  50. if($this->db_connect_id)
  51. {
  52. if($database != "")
  53. {
  54. $this->dbname = $database;
  55. $dbselect = mysqli_select_db($this->db_connect_id, $this->dbname);
  56. if(!$dbselect)
  57. {
  58. mysqli_close($this->db_connect_id);
  59. $this->db_connect_id = $dbselect;
  60. }
  61. }
  62. return $this->db_connect_id;
  63. }
  64. else
  65. {
  66. throw new \RuntimeException('Connection to mySQL failed!');
  67. }
  68. }
  69. //
  70. // Other base methods
  71. //
  72. function sql_close()
  73. {
  74. if($this->db_connect_id)
  75. {
  76. if($this->query_result)
  77. {
  78. @mysqli_free_result($this->query_result);
  79. }
  80. $result = mysqli_close($this->db_connect_id);
  81. return $result;
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. //
  89. // Base query method
  90. //
  91. function sql_query($query = "", $transaction = FALSE)
  92. {
  93. // Remove any pre-existing queries
  94. unset($this->query_result);
  95. if($query != "")
  96. {
  97. nt_common_add_debug($query);
  98. $this->num_queries++;
  99. $this->query_result = mysqli_query($this->db_connect_id, $query);
  100. }
  101. if($this->query_result)
  102. {
  103. return $this->query_result;
  104. }
  105. else
  106. {
  107. return ( $transaction == 'END_TRANSACTION' ) ? true : false;
  108. }
  109. }
  110. function sql_select_db($dbname)
  111. {
  112. if($this->db_connect_id)
  113. {
  114. $result = mysqli_select_db($this->db_connect_id, $dbname);
  115. return $result;
  116. }
  117. return false;
  118. }
  119. function sql_reselect_db()
  120. {
  121. if($this->db_connect_id)
  122. {
  123. $result = mysqli_select_db($this->db_connect_id, $this->dbname);
  124. return $result;
  125. }
  126. return false;
  127. }
  128. //
  129. // Other query methods
  130. //
  131. function sql_numrows($query_id = 0)
  132. {
  133. if(!$query_id)
  134. {
  135. $query_id = $this->query_result;
  136. }
  137. if($query_id)
  138. {
  139. $result = mysqli_num_rows($query_id);
  140. return $result;
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. }
  147. function sql_affectedrows()
  148. {
  149. if($this->db_connect_id)
  150. {
  151. $result = mysqli_affected_rows($this->db_connect_id);
  152. return $result;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. function sql_numfields($query_id = 0)
  160. {
  161. if(!$query_id)
  162. {
  163. $query_id = $this->query_result;
  164. }
  165. if($query_id)
  166. {
  167. $result = mysqli_num_fields($query_id);
  168. return $result;
  169. }
  170. else
  171. {
  172. return false;
  173. }
  174. }
  175. // function sql_fieldname($query_id = 0){}
  176. // function sql_fieldtype($offset, $query_id = 0){}
  177. function sql_fetchrow($query_id = 0)
  178. {
  179. if(!$query_id)
  180. {
  181. $query_id = $this->query_result;
  182. }
  183. if($query_id)
  184. {
  185. return mysqli_fetch_array($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. while($row = mysqli_fetch_array($query_id))
  201. {
  202. $result[] = $row;
  203. }
  204. return $result;
  205. }
  206. else
  207. {
  208. return false;
  209. }
  210. }
  211. // function sql_fetchfield($field, $rownum = -1, $query_id = 0){}
  212. // function sql_rowseek($rownum, $query_id = 0){}
  213. function sql_nextid(){
  214. if($this->db_connect_id)
  215. {
  216. $result = mysqli_insert_id($this->db_connect_id);
  217. return $result;
  218. }
  219. else
  220. {
  221. return false;
  222. }
  223. }
  224. function sql_freeresult($query_id = 0){
  225. if(!$query_id)
  226. {
  227. $query_id = $this->query_result;
  228. }
  229. if ( $query_id )
  230. {
  231. @mysqli_free_result($query_id);
  232. return true;
  233. }
  234. else
  235. {
  236. return false;
  237. }
  238. }
  239. function sql_escape_string($str)
  240. {
  241. return mysqli_real_escape_string($this->db_connect_id, $str);
  242. }
  243. function sql_error($query_id = 0)
  244. {
  245. $result["message"] = mysqli_error($this->db_connect_id);
  246. $result["code"] = mysqli_errno($this->db_connect_id);
  247. return $result;
  248. }
  249. } // class sql_db
  250. class sql_db_string extends sql_db
  251. {
  252. //
  253. // Constructor ($connstring format : mysql://user:password@host/dbname)
  254. //
  255. function __construct($connstring, $persistency = true)
  256. {
  257. $ret = false;
  258. if ($connstring != '')
  259. {
  260. if (preg_match("#^mysqli?://([^:]+)(?::([^@]*))?@([^\\/]+)/([^/]+)[/]?$#", $connstring, $params))
  261. {
  262. $sqlserver = $params[3];
  263. $sqluser = $params[1];
  264. $sqlpassword = $params[2];
  265. $database = $params[4];
  266. $sqlport = 0;
  267. $s = explode(":", $sqlserver, 2);
  268. if (sizeof($s) == 2)
  269. {
  270. $sqlserver = $s[0];
  271. $sqlport = $s[1];
  272. }
  273. $ret = parent::__construct($sqlserver, $sqlport, $sqluser, $sqlpassword, $database, $persistency);
  274. }
  275. }
  276. return $ret;
  277. }
  278. } // class sql_db_string
  279. } // if ... define