class.db.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. class DB
  3. {
  4. //개발환경
  5. // var $host='211.253.37.172';
  6. var $host = 'localhost';
  7. var $user='echajoadb'; // db user
  8. var $pass='dlck5309@'; // db password
  9. var $db ='echajoadb';
  10. var $isDebug = false;
  11. // db id
  12. function __construct() {
  13. @$this->conn=mysqli_connect($this->host, $this->user, $this->pass, $this->db);
  14. $this->query("set names utf8");
  15. }
  16. function DB()
  17. {
  18. // @$this->conn=mysql_connect($this->host, $this->user, $this->pass);
  19. // mysql_select_db($this->db, $this->conn)or die(mysql_error());
  20. // $this->query("set names utf8");
  21. @$this->conn=mysqli_connect($this->host, $this->user, $this->pass, $this->db);
  22. $this->query("set names utf8");
  23. }
  24. function query($query, $qnum=0)
  25. {
  26. if ($qnum) {
  27. $this->{'query'.$qnum}=$query;
  28. // return $this->{'result'.$qnum}=mysql_query($query,$this->conn);
  29. return $this->{'result'.$qnum}=mysqli_query($this->conn, $query);
  30. } else {
  31. $this->query=$query;
  32. // return $this->result=mysql_query($query,$this->conn);
  33. return $this->result=mysqli_query($this->conn, $query);
  34. }
  35. }
  36. function getOne($qnum=0)
  37. {
  38. return $qnum
  39. ? @mysql_result($this->{'result'.$qnum}, 0, 0)
  40. : @mysql_result($this->result, 0, 0);
  41. }
  42. function getRow($qnum=0)
  43. {
  44. return $qnum
  45. ? ($this->{'row'.$qnum}=mysql_fetch_assoc($this->{'result'.$qnum}))
  46. : ($this->row =mysql_fetch_assoc($this->result));
  47. }
  48. function getLastIndex()
  49. {
  50. return $this->lastIndex=mysql_insert_id($this->conn);
  51. }
  52. function insertDAO($_field, $_table)
  53. {
  54. $this->debug("INSERT INTO ".$_table ." set ".$this->_arrayField($_field));
  55. return $this->query("INSERT INTO ".$_table ." set ".$this->_arrayField($_field));
  56. }
  57. function selectDAO($_field, $_table, $_where = NULL, $_order = NULL)
  58. {
  59. if (is_array($_where)) {
  60. $_whereis = " WHERE ". $this->_where($_where, $value, ' AND ', $escape = TRUE);
  61. } else {
  62. if ($_where) $_whereis = " WHERE ". $_where;
  63. }
  64. if ($_order) $_orderis = " ORDER BY ". $_order;
  65. $this->query("SELECT ".$_field." FROM ".$_table . $_whereis . $_orderis);
  66. return $this->getRow($_num);
  67. }
  68. function updateDAO($_update, $_table, $_where, $_order = NULL) {
  69. if (is_array($_where)) {
  70. $_whereis = " WHERE ". $this->_where($_where, $value, ' AND ', $escape = TRUE);
  71. } else {
  72. if ($_where) $_whereis = " WHERE ". $_where;
  73. }
  74. if (is_array($_update))
  75. $_update = $this->_arrayField($_update);
  76. if ($_order) $_orderis = " ORDER BY ". $_order;
  77. $this->debug("UPDATE ".$_table." SET ".$_update . $_whereis . $_orderis);
  78. return $this->query("UPDATE ".$_table." SET ".$_update . $_whereis . $_orderis);
  79. }
  80. function deleteDAO($_table, $_where) {
  81. if ($_where) $_whereis = " WHERE ". $this->_where($_where, $value, ' AND ', $escape = TRUE);
  82. return $this->query("DELETE FROM ".$_table . $_whereis);
  83. }
  84. function getLastId(){
  85. return mysql_insert_id();
  86. }
  87. function getOneDAO($_field, $_table, $_where) {
  88. if (is_array($_where)) {
  89. $_whereis = ' WHERE ' . $this->_where($_where, $value, ' AND ', $escape = TRUE);
  90. } else {
  91. if ($_where) $_whereis = " WHERE" . $_where;
  92. }
  93. //echo "SELECT ".$_field." FROM ".$_table . $_whereis;
  94. $this->query("SELECT ".$_field." FROM ".$_table . $_whereis);
  95. return $this->getOne($_num);
  96. }
  97. function _arrayField($key, $type = ", ", $escape = TRUE)
  98. {
  99. if ( ! is_array($key))
  100. {
  101. $key = array($key => $value);
  102. }
  103. foreach ($key as $k => $v)
  104. {
  105. $prefix = (count($ar_where) == 0) ? '' : $type;
  106. if ($escape == TRUE)
  107. {
  108. $v = $this->escape(trim($v));
  109. }
  110. if ( ! $this->_has_operator($k))
  111. {
  112. $k .= ' = ';
  113. }
  114. $ar_where .= $prefix.$k.$v;
  115. }
  116. return $ar_where;
  117. }
  118. /**
  119. * Where
  120. *
  121. * Called by where() or orwhere()
  122. *
  123. * @access private
  124. * @param mixed
  125. * @param mixed
  126. * @param string
  127. * @return object
  128. */
  129. function _where($key, $value = NULL, $type = ' AND ', $escape = NULL)
  130. {
  131. if ( ! is_array($key))
  132. {
  133. $key = array($key => $value);
  134. }
  135. foreach ($key as $k => $v)
  136. {
  137. $prefix = (count($ar_where) == 0) ? '' : $type;
  138. if (is_null($v) && ! $this->_has_operator($k))
  139. {
  140. $k .= ' IS NULL';
  141. }
  142. if ( ! is_null($v))
  143. {
  144. if ($escape == TRUE)
  145. {
  146. $v = ' '.$this->escape(trim($v));
  147. }
  148. if ( ! $this->_has_operator($k))
  149. {
  150. $k .= ' = ';
  151. }
  152. }
  153. $ar_where .= $prefix.$k.$v;
  154. }
  155. return $ar_where;
  156. }
  157. /**
  158. * Tests whether the string has an SQL operator
  159. *
  160. * @access private
  161. * @param string
  162. * @return bool
  163. */
  164. function _has_operator($str)
  165. {
  166. $str = trim($str);
  167. if ( ! preg_match("/(\s|<|>|!|=|is null|is not null)/i", $str))
  168. {
  169. return FALSE;
  170. }
  171. return TRUE;
  172. }
  173. /**
  174. * "Smart" Escape String
  175. *
  176. * Escapes data based on type
  177. * Sets boolean and null types
  178. *
  179. * @access public
  180. * @param string
  181. * @return mixed
  182. */
  183. function escape($str)
  184. {
  185. $str = preg_replace('#<script.*?</script\s*>#is', '', $str);
  186. if (is_string($str))
  187. {
  188. $str = "'".$this->escape_str($str)."'";
  189. }
  190. elseif (is_bool($str))
  191. {
  192. $str = ($str === FALSE) ? 0 : 1;
  193. }
  194. elseif (is_null($str))
  195. {
  196. $str = 'NULL';
  197. }
  198. return $str;
  199. }
  200. /**
  201. * Escape String
  202. *
  203. * @access public
  204. * @param string
  205. * @param bool whether or not the string will be used in a LIKE condition
  206. * @return string
  207. */
  208. function escape_str($str, $like = FALSE)
  209. {
  210. if (is_array($str))
  211. {
  212. foreach($str as $key => $val)
  213. {
  214. $str[$key] = $this->escape_str($val, $like);
  215. }
  216. return $str;
  217. }
  218. $str = is_resource($this->conn_id) ? mysql_real_escape_string($str, $this->conn_id) : addslashes($str);
  219. // escape LIKE condition wildcards
  220. if ($like === TRUE)
  221. {
  222. $str = str_replace(array('%', '_'), array('\\%', '\\_'), $str);
  223. }
  224. return $str;
  225. }
  226. function close()
  227. {
  228. mysql_close($this->conn);
  229. }
  230. function debug($query){
  231. //echo $query;
  232. }
  233. }
  234. ?>