나만의 쓰기 편한 db클래스 구현(2.17 수정)

웹 & 안드로이드/PHP|2014. 2. 14. 17:05

 

- 2.17 mysql함수로 쿼리 결과 나오도록 수정

 

- db클래스

: 객체를 여러개 만들 필요가 없기 때문에 싱글톤으로 구현.

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
<?php 
class DB_mysql{
    /* singleton pattern. */
    private static $instance = null;
    
    private function __construct(){
    }
    public static function &getInstance(){
        if(self::$instance == null){
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /* Class start. */
    public $conn = null; #db connection info.
    
    //db connect.
    public function connect($host$id$pw$db_name){
        if(function_exists('mysqli_connect')){
            //mysqli function used.
            $this->conn = mysqli_connect($host$id$pw$db_name
                or die('error! db connect failed.<br />').mysqli_connect_error();
        }else{
            //mysql function used.
            $this->conn = mysql_connect($host$id$pw
                or die('error! db connect failed.<br />').mysql_error($this->conn);
            mysql_select_db($db_name$this->conn);
        }
    }
    
    //select query. $arr[row num][table field].
    public function query_of_select(&$sql){
 
        $arr = array();
        $result = $this->query($sql);
        
        if($result == ''return null;
        
        if(function_exists('mysqli_fetch_fields') and function_exists('mysqli_fetch_array')){
            $fieldsinfo = mysqli_fetch_fields($result);
            $row = null;
 
            $i = -1;
            do{
                foreach ($fieldsinfo as $field){
                    $arr[($i == -1)? 0: $i][$field->name] = ($i == -1)? 'null'$row[$field->name];
                }
                $i++;
            }while($row = mysqli_fetch_array($result));
            
            mysqli_free_result($result);
        
        }else{
            $fieldsinfo = array();
            $row = null;
 
            for($i = 0; $field = mysql_fetch_field($result); $i++){
                $fieldsinfo[$i] = $field;
            }
 
            $i = -1;
            do{
                foreach ($fieldsinfo as $field){
                    $arr[($i == -1)? 0: $i][$field->name] = ($i == -1)? 'null'$row[$field->name];
                }
                $i++;
            }while($row = mysql_fetch_array($result));
 
             mysql_free_result($result);#result 바로 자원 반납.
            
        }
        
        return $arr;
    }
    
    //insert, update, delete 나 create, alter, drop 등 결과가 없는 쿼리 전용.
    public function query_of_CUD(&$sql){
        return $this->query($sql);
    }
    
    //db에 쿼리 날림.
    private function query(&$sql){
        if(function_exists('mysqli_connect')){
            $result = mysqli_query($this->conn, $sql) and $sql = null; #db입력 후, sql변수 null로 초기화.
        }else{
            $result = mysql_query($sql$this->conn) and $sql = null;
        }
        return $result;
    }
    
    //db 접속 닫음.
    public function close(){
        function_exists('mysqli_close')
            ? mysqli_close($this->conn) 
            : mysql_close($this->conn);
    }
    
    //에러 출력.
    public function error(){
        echo function_exists('mysqli_error')
            ? mysqli_error($this->conn)
            : mysql_error($this->conn);
    }
 
    //테이블 형태로 출력.
    public static function print_result_totable($result){
        echo "<style>th{background-color:#aaaaaa;}td{border:black 1px solid;font-size:1em;}</style>";
        echo "<table>";
        echo "<tr><th></th>";
            foreach ($result[0] as $fieldname => $v){
                echo "<th>".$fieldname."</th>";
            }
            echo "</tr>";
        foreach ($result as $rownum => $row){
            echo "<tr><td>".($rownum+1)."</td>";
            foreach ($row as $value){
                echo "<td><center><xmp>".$value."</xmp></center></td>";
            }
            echo "</tr>";
        }
        echo "</table>";
    }
}
?>

 

-사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
$db = DB_mysql::getInstance();
$db->connect('host''db_id''db_pw''db_name');
 
$sql = "select * from sboard";
 
$result = $db->query_of_select($sql);
echo "<table>";
foreach ($result as $rownum => $row){
    echo "<tr><td>".$rownum."행</td>";
    foreach ($row as $field => $value){
        echo "<td>".$field." : ".$value."</td>";
    }
    echo "</tr>";
}
echo "</table>";
 
$db->close();
?>

 

 

댓글()