db.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. class mysql
  3. {
  4. public $query_num = 0;
  5. public $link;
  6. public function mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0)
  7. {
  8. $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
  9. }
  10. public function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0)
  11. {
  12. global $dbcharset;
  13. $func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
  14. if (!($this->link = @$func($dbhost, $dbuser, $dbpw, 1))) {
  15. $this->halt('Can not connect to MySQL server');
  16. }
  17. if ('4.1' < $this->server_info()) {
  18. if ($dbcharset != 'latin1') {
  19. mysql_query('SET character_set_connection=' . $dbcharset . ', character_set_results=' . $dbcharset . ', character_set_client=binary', $this->link);
  20. }
  21. if ('5.0.1' < $this->server_info()) {
  22. mysql_query('SET sql_mode=\'\'', $this->link);
  23. }
  24. }
  25. if ($dbname) {
  26. if (!@mysql_select_db($dbname, $this->link)) {
  27. $this->halt('Cannot use database ' . $dbname);
  28. }
  29. }
  30. }
  31. public function select_db($dbname)
  32. {
  33. $this->dbname = $dbname;
  34. if (!@mysql_select_db($dbname, $this->link)) {
  35. $this->halt('Cannot use database ' . $dbname);
  36. }
  37. }
  38. public function server_info()
  39. {
  40. return mysql_get_server_info();
  41. }
  42. public function version()
  43. {
  44. return mysql_get_server_info();
  45. }
  46. public function fetchRow($query)
  47. {
  48. return mysql_fetch_assoc($query);
  49. }
  50. public function fetch_first($sql)
  51. {
  52. return $this->fetch_array($this->query($sql));
  53. }
  54. public function query($SQL, $method = '')
  55. {
  56. if ($method == 'unbuffer' && function_exists('mysql_unbuffered_query')) {
  57. $query = mysql_unbuffered_query($SQL, $this->link);
  58. }
  59. else {
  60. $query = mysql_query($SQL, $this->link);
  61. }
  62. if (!$query && $method != 'SILENT') {
  63. $this->halt('MySQL Query Error: ' . $SQL);
  64. }
  65. $this->query_num++;
  66. return $query;
  67. }
  68. public function getOne($sql, $limited = false)
  69. {
  70. if ($limited == true) {
  71. $sql = trim($sql . ' LIMIT 1');
  72. }
  73. $res = $this->query($sql);
  74. if ($res !== false) {
  75. $row = mysql_fetch_row($res);
  76. if ($row !== false) {
  77. return $row[0];
  78. }
  79. else {
  80. return '';
  81. }
  82. }
  83. else {
  84. return false;
  85. }
  86. }
  87. public function get_one($sql, $result_type = MYSQL_ASSOC)
  88. {
  89. $result = $this->query($sql);
  90. $record = $this->fetch_array($result, $result_type);
  91. return $record;
  92. }
  93. public function get_value($sql, $result_type = MYSQL_NUM, $field = 0)
  94. {
  95. $result_set = $this->query($sql);
  96. $rt = &$this->fetch_array($result_set, $result_type);
  97. return isset($rt[$field]) ? $rt[$field] : false;
  98. }
  99. public function escape($s)
  100. {
  101. if (function_exists('mysql_real_escape_string')) {
  102. return htmlspecialchars(mysql_real_escape_string($s, $this->link));
  103. }
  104. return htmlspecialchars(addslashes($s));
  105. }
  106. public function query_unbuffered($sql)
  107. {
  108. $s = $this->query($sql, 'UNBUFFERED');
  109. return $s;
  110. }
  111. public function getCol($sql)
  112. {
  113. $res = $this->query($sql);
  114. if ($res !== false) {
  115. $arr = array();
  116. while ($row = mysql_fetch_row($res)) {
  117. $arr[] = $row[0];
  118. }
  119. return $arr;
  120. }
  121. else {
  122. return false;
  123. }
  124. }
  125. public function getAll($sql)
  126. {
  127. $res = $this->query($sql);
  128. if ($res !== false) {
  129. $arr = array();
  130. while ($row = mysql_fetch_array($res)) {
  131. $arr[] = $row;
  132. }
  133. return $arr;
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139. public function getRow($sql, $limited = false)
  140. {
  141. if ($limited == true) {
  142. $sql = trim($sql . ' LIMIT 1');
  143. }
  144. $res = $this->query($sql);
  145. if ($res !== false) {
  146. return mysql_fetch_assoc($res);
  147. }
  148. else {
  149. return false;
  150. }
  151. }
  152. public function fetch_array($query, $result_type = MYSQL_ASSOC)
  153. {
  154. return mysql_fetch_array($query, $result_type);
  155. }
  156. public function affected_rows()
  157. {
  158. return mysql_affected_rows($this->link);
  159. }
  160. public function fetch_row($query)
  161. {
  162. return mysql_fetch_row($query);
  163. }
  164. public function num_rows($query)
  165. {
  166. return mysql_num_rows($query);
  167. }
  168. public function num_fields($query)
  169. {
  170. return mysql_num_fields($query);
  171. }
  172. public function result($query, $row)
  173. {
  174. $query = mysql_result($query, $row);
  175. return $query;
  176. }
  177. public function free_result($query)
  178. {
  179. return mysql_free_result($query);
  180. }
  181. public function insert_id()
  182. {
  183. return 0 <= ($id = mysql_insert_id($this->link)) ? $id : $this->result($this->query('SELECT last_insert_id()'), 0);
  184. }
  185. public function Close()
  186. {
  187. return mysql_close($this->link);
  188. }
  189. public function error()
  190. {
  191. return $this->link ? mysql_error($this->link) : mysql_error();
  192. }
  193. public function errno()
  194. {
  195. return intval($this->link ? mysql_errno($this->link) : mysql_errno());
  196. }
  197. public function halt($msg = '')
  198. {
  199. global $charset;
  200. $msg = '<html>
  201. <head>
  202. ';
  203. $msg .= '<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  204. ';
  205. $msg .= '<style type="text/css">
  206. ';
  207. $msg .= 'body,p,pre {
  208. ';
  209. $msg .= 'font:12px Verdana;
  210. ';
  211. $msg .= '}
  212. ';
  213. $msg .= '</style>
  214. ';
  215. $msg .= '</head>
  216. ';
  217. $msg .= '<body bgcolor="#FFFFFF" text="#000000" link="#006699" vlink="#5493B4">
  218. ';
  219. $msg .= '<b>MayiCMS error</b>: ' . htmlspecialchars($this->error()) . '
  220. <br />';
  221. $msg .= '<b>error number</b>: ' . $this->errno() . '
  222. <br />';
  223. $msg .= '<b>Date</b>: ' . date('Y-m-d @ H:i') . '
  224. <br />';
  225. $msg .= '<b>Script</b>: http://' . $_SERVER['HTTP_HOST'] . getenv('REQUEST_URI') . '
  226. <br />';
  227. $msg .= '</body>
  228. </html>';
  229. echo $msg;
  230. exit();
  231. }
  232. }
  233. !defined('IN_MYMPS') && exit('FORBIDDEN');
  234. $db = new mysql($db_host, $db_user, $db_pass, $db_name);
  235. $db_host = $db_user = $db_pass = NULL;
  236. ?>