mysql.class.php

Gesponsorde koppelingen

PHP script bestanden

  1. mysql.class.php

« 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?PHP
/*
 * MySQL class (MySQLi replacement)
 * @author SilverWolf
 * @version 1.0
 * @package MySQL
 *
 */


class MySQL{
    /**
     * Contains the error message if there was a connection problem
     * @access public
     * @var string
     */

    public $connect_error;
    
    /**
     * Contains the error code if there was a connection problem
     * @access public
     * @var int
     */

    public $connect_errno;
    
    /**
     * Contains the error code if there was an error
     * @access public
     * @var string
     */

    public $error;
    
    /**
     * Contains the error code if there was an error
     * @access public
     * @var int
     */

    public $errno=0;
    
    /**
     * Contains detailed info about the last query
     * @access public
     * @var string
     */

    public $info;
    
    /**
     * Contains the number of affected rows in the last query (same as mysql_num_rows in a SELECT statement)
     * @access public
     * @var int
     */

    public $affected_rows;
    
    /**
     * Contains the number of fields in the last query
     * @access public
     * @var int
     */

    public $field_count=0;
    
    /**
     * Contains the mysql connection to the database
     * @access private
     * @var resource of type mysql link
     */

    private $connection;
    
    /**
     * Contains all the queries of the last multiquery
     * @access private
     * @var array
     */

    private $multi_query_queries=array();
    
    /**
     * Contains the offset of the multiquery array, used when fetching next result
     * @access private
     * @var int
     */

    private $mutli_query_offset=0;
    
    /**
     * Contains the result of the last query executed
     * @access private
     * @var MySQL_result
     */

    private $multi_query_result;
    
    /*
     * The constructor, sets the mysql resource link if data is given
     * @param $host string
     * @param $user string
     * @param $pass string
     * @param $db string
     */

    public function __construct($host=null,$user=null,$pass=null,$db=null){
        if(empty($host)){
            return;
        }
else{
            $this->connect($host,$user,$pass,$db);
            return;
        }
    }

    
    /*
     * The connector, this actually makes the connection with the database
     * @param $host string
     * @param $user string
     * @param $pass string
     * @param $db string
     */

    public function connect($host,$user,$pass,$db){
        $this->connection=mysql_connect($host,$user,$pass);
        if(!$this->connection){
            $this->set_error("connection");
            $this->connect_error=mysql_error($this->connection);
            $this->connect_errno=mysql_errno($this->connection);
            return;
        }
else{
            $dbselect=mysql_select_db($db,$this->connection);
            if(!$dbselect){
                $this->connect_error=mysql_error($this->connection);
                $this->connect_errno=mysql_errno($this->connection);
                return;
            }
else{
                return;
            }
        }
    }

    
    /*
     * Set an error
     * @param $type string
     */

    public function set_error($type){
        if($type=="connection"){
            $this->error=mysql_error($this->connection);
            $this->errno=mysql_errno($this->connection);
            return;
        }
else{
            //Space for other types of errors (STMT)
            return;
        }
    }

    
    /*
     * Execute given query
     * @param $query string
     * @return MySQL_result
     * @return boolean
     */

    public function query($query){
        $this->last_query_result=mysql_query($query,$this->connection);
        if($this->last_query_result!==false){
            $this->affected_rows=max(mysql_num_rows($this->last_query_result),mysql_affected_rows($this->last_query_result));
            $this->info=mysql_info($this->connection);
            $insert_id=mysql_insert_id($this->connection);
            $this->field_count=@count(mysql_fetch_row($this->last_query_result));
            mysql_data_seek($this->last_query_result,0);
            return new MySQL_result($this->last_query_result,$insert_id);
        }
else{
            $this->set_error("connection");
            return false;
        }
    }

    
    /*
     * Execute given multiquery
     * @param $queries string
     * @return boolean
     */

    public function multi_query($queries){
        $this->multi_query_queries=explode(";",$queries);
        $this->last_query_result=$this->query($this->multi_query_queries[$this->multi_query_offset]);
        $this->multi_query_offset++;
        if($this->errno!==0){
            return false;
        }
else{
            return true;
        }
    }

    
    /*
     * Store the current result of a multiquery
     * @return MySQL_result
     */

    public function store_result(){
        return $this->last_query_result;
    }

    
    /*
     * Check if there are more results available in a multiquery
     * @return boolean
     */

    public function more_results(){
        return ($this->multi_query_offset<count($this->multi_query_queries));
    }

    
    /*
     * Prepare the next result of a multiquery
     * @return boolean
     */

    public function next_result(){
        $this->last_query_result=$this->query($this->multi_query_queries[$this->multi_query_offset]);
        $this->multi_query_offset++;
        if(($this->errno!==0)&&(!$this->last_query_result)){
            return false;
        }
else{
            return true;
        }
    }

    
    /*
     * Prepare the next result of a multiquery
     * @param $data mixed
     * @return mixed
     */

    public function real_escape($data){
        if(is_array($data)){
            foreach($data as &$value){
                $value=mysql_real_escape_string($data,$this->connection);
            }

            return $data;
        }
else{
            return mysql_real_escape_string($data,$this->connection);
        }
    }

    
    /*
     * Prepare a query
     * @param $query string
     * @return MySQL_STMT
     */

    public function prepare($query){
        return new MySQL_STMT($query,$this);
    }

    
    /*
     * Close the mysql-connection
     * @return boolean
     */

    public function close(){
        return mysql_close($this->connection);
    }

    
    /*
     * Checks whether the connection to the server is working
     * @return boolean
     */

    public function ping(){
        return mysql_ping($this->connection);
    }

    
    /*
     * Sets the current active database
     * @return boolean
     */

    public function select_db($dbname){
        return mysql_select_db($dbname,$this->connection);
    }
}


/*
 * MySQL_result class (MySQLi_result replacement)
 * @author SilverWolf
 * @version 1.0
 * @package MySQL_result
 *
 */

class MySQL_result{
    /**
     * Contains the result given by mysql_query()
     * @access protected
     * @var resource of type mysql result
     */

    protected $result;
    
    /**
     * If the result is freed, it cannot be fetched anymore
     * @access protected
     * @var resource of type mysql result
     */

    protected $is_freed=false;
    
    /**
     * Contains the number of rows if query was a SELECT query
     * @access public
     * @var int
     */

    public $num_rows;
    
    /**
     * Contains the number of fields
     * @access public
     * @var int
     */

    public $field_count;
    
    /*
     * The constructor, puts the result, number of rows and id in the appropriate vars
     * @param $host string
     * @param $user string
     * @param $pass string
     * @param $db string
     */

    public function __construct($result,$id=null){
        $this->result=$result;
        $this->num_rows=mysql_num_rows($this->result);
        $this->field_count=mysql_num_fields($this->result);
        return;
    }

    
    /*
     * Returns an associative array containing the current result row
     * @return array
     */

    public function fetch_assoc(){
        if(!$this->is_freed){
            return mysql_fetch_array($this->result,MYSQLI_ASSOC);
        }
else{
            return false;
        }
    }

    
    /*
     * Returns both an asociative and a numeric array containing the current result row
     * @return array
     */

    public function fetch_array($resulttype=MYSQLI_BOTH){
        if(!$this->is_freed){
            return mysql_fetch_array($this->result,$resulttype);
        }
else{
            return false;
        }
    }

    
    /*
     * Returns a numeric array containing the current result row
     * @return array
     */

    public function fetch_row(){
        if(!$this->is_freed){
            return mysql_fetch_array($this->result,MYSQLI_NUM);
        }
else{
            return false;
        }
    }

    
    /*
     * Returns an object containing the current result row
     * @return array
     */

    public function fetch_object(){
        if(!$this->is_freed){
            return mysql_fetch_object($this->result);
        }
else{
            return false;
        }
    }

    
    /*
     * Seek an arbitrary result pointer specified by the offset in the result set
     * @return bool
     */

    public function data_seek($offset){
        return mysql_data_seek($this->result,$offset);
    }

    
    /*
     * Frees the memory associated with a result, clears the object
     * @return bool
     */

    public function free(){
        mysql_free_result($this->result);
        $this->result=null;
        $this->is_freed=true;
        $this->num_rows=null;
        $this->field_count=null;
        return;
    }

    
    /*
     * Sets the field cursor to the given offset
     * @return bool
     */

    public function field_seek($offset){
        return mysql_field_seek($this->result,$offset);
    }

    
    /*
     * Returns an array of objects which contains field definition information
     *
     * PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
     * AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
     * THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
     *
     * @return array
     */

    public function fetch_fields(){
        $data=array();
        $fields=mysql_num_fields($this->result);
        for($i=0;$i<$fields;$i++){
            $data[]=mysql_fetch_field($this->result,$i);
        }

        return $data;
    }

    
    /*
     * Returns the definition of one column of a result set as an object
     *
     * PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
     * AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
     * THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
     *
     * @return object
     */

    public function fetch_field(){
        return mysql_fetch_field($this->result);
    }

    
    /*
     * Fetch meta-data for a single field
     *
     * PLEASE PAY ATTENTION! THIS FUNCTION DOES NOT RETURN THE SAME VALUES AS THE MYSQLI CLASS,
     * AND SHOULD BE IMPLEMENTED OTHERWISE! WHEN USING, ALLWAYS CONSULT THE PHP MANUAL ABOUT
     * THE MYSQL AND MYSQLI FUNCTIONS ( mysql_fetch_field() and mysqli_fetch_field() )!
     *
     * @return object
     */

    public function fetch_field_direct($offset){
        if(!$this->field_seek($offset))return false;
        return $this->fetch_field();
    }
}


/* Define the constants used in the classes
 */

if(!defined("MYSQLI_ASSOC")){
    define("MYSQLI_ASSOC",1);
    define("MYSQLI_NUM",2);
    define("MYSQLI_BOTH",3);
}


/*
 * MySQL_STMT class (MySQLi_STMT replacement)
 * @author SilverWolf
 * @version 1.0
 * @package MySQL_STMT
 * @info:
   This package has not been finished yet. Please don't use prepared statements yet!
 */

class MySQL_STMT{
    public $affected_rows;
    public $num_rows;
    public $errno;
    public $error;
    public $field_count;
    public $param_count=0;
    
    protected $query;
    protected $prequery;
    protected $original_query;
    protected $link;
    protected $result;
    protected $bound_vars=array();
    public function __construct($query,&$link){
        $this->link=&$link;
        $this->prepare($query);
    }

    public function prepare($query){
        $this->prequery=explode("?",$query);
        $this->original_query=$this->prequery;
        $this->query=array_shift($this->prequery);
        return;
    }

    public function bind_param(){
        $args=func_get_args();
        $types=strtolower(array_shift($args));
        if(count($args)<1){
            $this->link->set_error("other","$mysql_stmt->bind_param() did not get enough arguments!");
        }
elseif(count($args)==1){
            if(is_array($args[0])){
                foreach($args[0] as $arg){
                    $this->add_param($arg,$types);
                }
            }
else{
                $this->add_param($args,$types);
            }
        }
else{
            $i=0;
            foreach($args as $arg){
                if(is_array($args)){
                    foreach($args as $arg){
                        $this->add_param($arg,$types[$i]);
                        $i++;
                    }
                }
else{
                    $this->add_param($args,$types[$i]);
                    $i++;
                }
            }
        }

        return;
    }

    
    protected function add_param($value,$type){
        if($type=="s"){
            $this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
        }
elseif($type=="i"){
            $this->query.=intval($value).array_shift($this->prequery);
        }
elseif($type=="d"){
            $this->query.=doubleval($value).array_shift($this->prequery);
        }
elseif($type=="b"){
            $this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
        }
else{
            $this->query.="'".$this->link->real_escape($value)."'".array_shift($this->prequery);
        }

        return;
    }

    
    public function bind_result(&$a,&$b=null,&$c=null,&$d=null,&$e=null,&$f=null,&$g=null,&$h=null,&$i=null,&$j=null,&$k=null,&$l=null,&$m=null,&$n=null,&$o=null,&$p=null,&$q=null,&$r=null,&$s=null,&$t=null,&$u=null,&$v=null,&$w=null,&$x=null,&$y=null,&$z=null){
        $vars=range('a','z');
        foreach($vars as $var){
            $this->bound_vars[]=&${$var};
        }
    }

    
    public function fetch() {
        $row = $this->result->fetch_row();
        if($row==false){
            return false;
        }
else{
            foreach($row as $index => $value){
                $this->bound_vars[$index] = $value;
                $this->param_count++;
            }

            return true;
        }
    }

    
    public function execute(){
        $this->result=$this->link->query($this->query);
        //asdfasdfasdf
            $this->num_rows=$this->result->num_rows;
            $this->affected_rows=$this->link->affected_rows;
            $this->insert_id=$this->link->insert_id;
            $this->field_count=$this->result->field_count;
        return;
    }

    
    public function data_seek($offset){
        return $this->result->data_seek($offset);
    }

    
    public function close(){
        unset($this);
        return;
    }

    
    public function store_result(){
        return $this->result;
    }
}

?>

 
 

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.