db-mysql-connection-class

Gesponsorde koppelingen

PHP script bestanden

  1. db-mysql-connection-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php

class cConnection {
    #$sLink is the link returned by mysql_connect()
    var $sLink;
    #$rRes is the resultset returned by $this->dbQuery()
    var $rRes;
    

    #var $bLogQueries shows wether queries should be logged or not
    var $bLogQueries;
    #var $iQueries holds the number of queries on this page, if $bLogQueries = true
    var $iQueries;
    #var $aQueries holds all queries, if $bLogQueries = true
    var $aQueries;

    /*
    constructor cConnection makes a mysql connection to the mysql server
    set variables
    */

    
    function cConnection($sDB,$sUser = "",$sPass = "",$sHost = "localhost",$bLogQueries = false){
        $this->sLink = @mysql_connect($sHost,$sUser,$sPass);
        if (!$this->sLink) {
            echo mysql_error();
            die;
        }


        if (!@mysql_select_db($sDB,$this->sLink)) {
            echo mysql_error($this->sLink);
            die;
        }

        $this->iQueries = 0;
        $this->bLogQueries = $bLogQueries;
    }


    /*
    dbQuery executes a query on the db selected by $this->cConnection,
    if the querie fails the mysql_error is shown and script execution
    is aborted. If $this->bLogQueris = true the query counter is increased
    and the query is added to the query array.
    dbQuery($sQuery) returns a mysql resultset if succesful and die's with a
    msyql error if it fails
    */


    function dbQuery($sQuery) {
        #make connection if it's the first query
        if (!isset($this->sLink) || $this->sLink == "") $this->cConnect($this->sDB,$this->sUser,$this->sPass,$this->sHost,$this->bLog);

        $rResult = mysql_query($sQuery,$this->sLink);
        if(!$rResult) {
            echo mysql_error($this->sLink);
            die;
        }

        if ($this->bLogQueries) {
            $this->aQueries[] = $sQuery;
            $this->iQueries++;
        }

        $this->rRes = $rResult;
        return $rResult;
    }


    /*
    $rResult is the resultset retrieved by a call to $this->dbQuery or
    empty if you want to use the last dbQuery or a query string if you
    want to execute that query first. Returns a array fetched by $iType, and pushes it all on 1
    stack
    */


    function dbFetchSmarty ( $rResult, $iType ) {
      if( !is_resource($rResult) )
        if(substr(strtoupper($rResult),0,6) == "SELECT")
          $rResult = $this->dbQuery( $rResult );

      if ( !$rResult ) {
        echo mysql_error($this->sLink);
        die;
      }
else {
        $aReturn = array();
        while ( $aFetched = mysql_fetch_array( $rResult , $iType ) ) array_push( $aReturn, $aFetched );
        return $aReturn;
      }
    }


    /*


    */


    function dbFetchResult ( $rResult, $iRow = 0, $mField = 0 ) {
      if( !is_resource($rResult) )
        if(substr(strtoupper($rResult),0,6) == "SELECT")
          $rResult = $this->dbQuery( $rResult );

      if ( !$rResult ) {
        echo mysql_error($this->sLink);
        die;
      }
else {
        return mysql_result ( $rResult, $iRow, $mField );
      }
    }


    /*
    $rResult is the resultset retrieved by a call to $this->dbQuery or
    empty if you want to use the last dbQuery or a query string if you
    want to execute that query first. Returns a mysql error if the query
    fails, returns an integer mysql_num_rows() otherwise
    */


       function dbRows($rResult = false){
        if(!is_resource($rResult)){
            if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
            else return mysql_num_rows($this->rRes);
        }


        if(!$rResult) {
            echo mysql_error($this->sLink);
            die;
        }
else return mysql_num_rows($rResult);
    }


    /*
    accepts a sql query or resultset and returns fetched row
    */

    
    function dbFetchRow ($rResult = false) {
        if(!is_resource($rResult)){
            if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
            else return mysql_fetch_row($this->rRes);
        }


        if(!$rResult) {
            echo mysql_error($this->sLink);
            die;
        }
else return mysql_fetch_row($rResult);
    }

    function
dbFetchArray ( $rResult = false, $iType ) {
        if(!is_resource($rResult)){
            if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
            else return mysql_fetch_array($this->rRes, $iType);
        }


        if(!$rResult) {
            echo mysql_error($this->sLink);
            die;
        }
else return mysql_fetch_array($rResult, $iType);
    }



    /*
    returns the mysql_affected_rows of the last query
    */


    function dbAffectedRows() {
        return mysql_affected_rows($this->sLink);
    }


    /*
    $rResult is a resultset retrieved by $this->dbQuery or false if you want
    to use the last dbQuery or a query string if you want to execute that query
    first. Returns a mysql_error if the query fails, returns a fetched object
    otherwise.
    */


    function dbFetchObject($rResult = false){
        if (!is_resource($rResult)){
            if(substr(strtoupper($rResult),0,6) == "SELECT") $rResult = $this->dbQuery($rResult);
            else $rResult = $this->rRes;
        }


        if(!$rResult) {
            echo mysql_error($this->sLink);
            die;
        }
else return mysql_fetch_object($rResult);
    }


    /*
    if $this->bLogQueries = true, dbOutputQueries() outputs all used queries
    on this page, returns nothing otherwise.
    */


    function dbOutputQueries() {
        if ($this->bLogQueries) foreach ($this->aQueries as $sValue) echo $sValue.'<br />';
    }


    /*
    returns the auto_increment column value for your last insert query,
    returns 0 if no auto_increment column was found or the query was no
    insert query
    */


    function dbInsertID() {
        return mysql_insert_id($this->sLink);
    }


    /*
    closes the connection to the mysql server
    */


    function dbClose(){
        mysql_close($this->sLink);
    }


    /*
    frees $rResult's memory
    */


    function dbFreeResult($rResult){
        mysql_free_result($rResult);
    }

}


$oCon = new cConnection("db","user","pass","localhost", false);
[
/code]

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.