/home/mjc1/public_html/onlinecs/mobile/libs/db_class.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php 
/*
**************************************************************************************
* Maker : 정영훈(m4700q@nate.com)                                                     *
* Create Date : 2010.1.18                                                             *
* Update Date : 2010.1.26                                                             *
* Create Purpose : DB Query 처리를 위한 클래스                                         *
* Program Version : 2.0                                                                 *
* Program Name : Soyou DB 클래스                                                     *
* Update 내용 :    기존의 사용방식을 단순화 시킴                                         *
**************************************************************************************
*/
class MySQL 
    var 
$_resource null;
    var 
$isdev;
    
    public function 
MySQL($connect){
        
$this->_resource NULL;
        
$this->isdev 1;
        
$this->connect($connect);
    }
    
    
//DB Connection 
    
public function connect($connect) {
        
/*$host = isget($connect, 'host');
        $user = isget($connect, 'user');
        $pass = isget($connect, 'pass');
        $name = isget($connect, 'name');*/
        
        
$host $connect['host']; // 배열로 수정
        
$user $connect['user'];
        
$pass $connect['pass'];
        
$name $connect['name'];        
        if (
$host && $user && $pass && $name) {
            
$resource = @mysql_connect($host$user$pass) or exit('db connect error');
            @
mysql_select_db($name$resource) or exit('db select error');
            
$this->_resource $resource;
            
$this->query("set names utf8");
        }
    }

    public function 
parse($param) {
        
$result = array('field'=>'*''where'=>null'group'=>null'order'=>null'limit'=>null);
        if (!
is_array($param)) return $result;
        if (
array_key_exists('field'$param) && $param['field']) $result['field'] = $param['field'];
        if (
array_key_exists('where'$param) && $param['where']) $result['where'] = 'WHERE ' $param['where'];
        if (
array_key_exists('group'$param) && $param['group']) $result['group'] = 'GROUP BY ' $param['group'];
        if (
array_key_exists('order'$param) && $param['order']) $result['order'] = 'ORDER BY ' $param['order'];
        if (
array_key_exists('limit'$param) && $param['limit']) $result['limit'] = 'LIMIT ' $param['limit'];
        return 
$result;
    }
    
    public function 
fetch($resrc=null) {
        if (!
$resrc) return false;
        return 
mysql_fetch_assoc($resrc);
        
//return mysql_fetch_array($resrc);
    
}

    public function 
querystring($args=null) {
        if (
is_array($args)) {
            
$data = array();
            foreach (
$args as $k => $v) {
                if (
preg_match('/(now\(\))/i'$v)) {
                    
$data[] = "`$k`={$v}";
                } else if(
preg_match('/([\+\-\*\/])=[0-9]+/'$v)) {
                    
$data[] = "`$k`={$v}";
                } else {
                    
$v str_replace(array("&""<"), array("&amp;""&lt;"), addslashes(stripslashes($v)));
                    
$data[] = "`$k`='{$v}'";
                }
            }
            return 
implode(', '$data);
        }
        return 
$args;
    }

    
// Update Query 처리
    
public function update($table$field$where=null){
        
$field $this->querystring($field);
        if (
$where$where 'WHERE ' $where;
        
$query trim("UPDATE " $table " SET {$field} {$where}");
        return 
$this->query($query);
    }

    
// Insert Query 처리 
    
public function insert($table$field){ 
        
$field $this->querystring($field);
        
$query trim("INSERT INTO " $table " SET {$field}");
        return 
$this->query($query);
    }

    
// Select Query 처리
    
public function select($table$param=array()){ 
        
extract($this->parse($param));
        
$query trim("SELECT {$field} FROM " $table {$where} {$group} {$order} {$limit}");
        
//prt($query);
        
$resrc $this->query($query);
        if (!
$resrc) return array();
        return 
$this->fetch_all($resrc);
   }
    public function 
fetch_all($stmt) {
        if(!
$stmt) return array();
        
$rowset = array();
        while(
$row $this->fetch($stmt)) {
            
$rowset[] = $row;
            
//echo($row[name]);
        
}
        return 
$rowset;
     }

    public function 
row($table$param=array()) {
        
extract($this->parse($param));
        
$sql trim("SELECT {$field} FROM " $table {$where} {$group} {$order} {$limit}");
        return 
$this->fetch($this->query($sql));
    }

    
// Delete Query 처리
    
public function delete($table$where){ 
        
$where 'WHERE ' $where;
        
$query trim("DELETE FROM " $table {$where}");
        return 
$this->query($query);
    }

    
// 쿼리를 받아서 최종 처리
    
public function query($query){ 
        
//return @mysql_query($query,$this->resource);
        
$res = @mysql_query($query$this->_resource) or exit('db query error' . (isdev() ? "[".mysql_errno()."] : {$query}<br><b>".mysql_error()."</b>" ''));
        if (!
$res) return false;
        
$type strtolower(substr($query06));
        if (
$type == 'select') return (mysql_num_rows($res)) ? $res false;
        if (
$type == 'update') return intval(mysql_affected_rows($this->_resource));
        if (
$type == 'delete') return intval(mysql_affected_rows($this->_resource));
        if (
$type == 'insert') {
            
$insert_id mysql_insert_id($this->_resource);
            return 
$insert_id $insert_id intval(mysql_affected_rows($this->_resource));
        }
        return 
$res;
    }

    
    
//하나의 값만 불러옴
    
public function one($table$field$where=null) {
        if (
$where$where 'WHERE ' $where;
        
$sql trim("SELECT {$field} FROM " $table {$where}");
        if (
$resrc $this->query($sql)) return mysql_result($resrc0);
        return 
false;
    }

    
//COUNT 값 
    
public function num($table$field$where=null$group=null) {
        
$field 'COUNT(' $field ')';
        if (
$where$where 'WHERE ' $where;
        if (
$group$group    =    'group by '$group;
        
$sql trim("SELECT {$field} FROM " $table {$where}".{$group}");
        
//echo $sql;
        
if ($resrc $this->query($sql)) return mysql_result($resrc0);
        return 
false;
    }

    
//레코드삭제
    
public function rDel($table$where){
        
$where 'where '.$where;
        
$sql trim("delete from "$table.{$where} ");
        
$this->query($sql);
        return 
false;
    }

    
//MAX 값
    
public function max($table$field$where=null) {
        if (
$where$where 'WHERE ' $where;
        
$sql trim("SELECT MAX(`{$field}`) FROM " $table {$where}");
        if (
$resrc $this->query($sql)) return mysql_result($resrc0);
        return 
false;
    }

    
//MIN 값
    
public function min($table$field$where=null) {
        if (
$where$where 'WHERE ' $where;
        
$sql trim("SELECT MIN(`{$field}`) FROM " $table {$where}");
        if (
$resrc $this->query($sql)) return mysql_result($resrc0);
        return 
false;
    }

}
/* insert 사용법
include 'classes/db_class.php';

$connect = array();
$connect['host'] = 'localhost'; // 호스트명
$connect['user'] = 'root'; // 디비 유저명
$connect['pass'] = '1234'; // 디비 접속 암호
$connect['name'] = 'test2'; // 데이타베이스명

$db = new MySQL($connect);
$array = $db->insert('member', array(
 'id' => 'test2',
 'name' => '한글'
));
*/

/* update 사용법
include 'classes/db_class.php';

$connect = array();
$connect['host'] = 'localhost'; // 호스트명
$connect['user'] = 'root'; // 디비 유저명
$connect['pass'] = '1234'; // 디비 접속 암호
$connect['name'] = 'test2'; // 데이타베이스명

$db = new MySQL($connect);
$array = $db->update('member', array(
 'id' => 'test200002',
 'name' => '한글2'
),'idx = 10');
*/

/* select 사용법
include 'classes/db_class.php';

$connect = array();
$connect['host'] = 'localhost'; // 호스트명
$connect['user'] = 'root'; // 디비 유저명
$connect['pass'] = '1234'; // 디비 접속 암호
$connect['name'] = 'test2'; // 데이타베이스명

$db = new MySQL($connect);
$array = $db->select('member', array(
 'field' => '*',
 'where' => 'idx = 10',
 'group' => '',
 'order' => 'idx desc',
 'limit' => ''
));
for($i=0;$i<count($array);$i++) {
    echo $array[$i]['id']." ";
    echo $array[$i]['name']."<br>";
}
*/

/* delete 사용법
include 'classes/db_class.php';

$connect = array();
$connect['host'] = 'localhost'; // 호스트명
$connect['user'] = 'root'; // 디비 유저명
$connect['pass'] = '1234'; // 디비 접속 암호
$connect['name'] = 'test2'; // 데이타베이스명

$db = new MySQL($connect);
$array = $db->delete('member', 'idx = 8');
*/