Source of trunk/l1_mysql.php at revision HEAD (05/06/2010 11:05:20, 28858 bytes, 908 lines, language: php) [download]:

1
<?php
2
3
/*
4
** L1 MySQL Driver version 2.0.1
5
** L1 SQL version 2.0
6
**
7
** http://projects.codewise.org/l1_sql/
8
**
9
** by William R. Fraser <wrf@codewise.org>
10
** Copyright (c) 2004-2009 Codewise.org
11
*/
12
13
/*
14
** This file is part of L1 SQL
15
**
16
** L1 SQL is free software; you can redistribute it and/or modify
17
** it under the terms of the GNU General Public License as published by
18
** the Free Software Foundation; either version 2 of the License, or
19
** (at your option) any later version.
20
**
21
** L1 SQL is distributed in the hope that it will be useful,
22
** but WITHOUT ANY WARRANTY; without even the implied warranty of
23
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
** GNU General Public License for more details.
25
**
26
** You should have received a copy of the GNU General Public License
27
** along with L1 SQL; if not, write to the Free Software
28
** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29
*/
30
31
if (!function_exists("mysql_connect")) {
32
    die("MySQL PHP extension not installed!");
33
}
34
35
if(!defined("L1SQL_ASSOC")) define("L1SQL_ASSOC",1);
36
if(!defined("L1SQL_NUM"))   define("L1SQL_NUM",  2);
37
if(!defined("L1SQL_BOTH"))  define("L1SQL_BOTH"3);
38
39
if(!class_exists("L1_MySQL")) {
40
class L1_MySQL
41
{
42
    var $set false//true if connected and database set
43
44
    var $hostname "";
45
    var $username "";
46
    var $password "";
47
    var $database "";
48
    var $session null;
49
50
    var $error_no 0;
51
    var $error_str "";
52
    var $halt_on_error true//if true, stop on an error
53
54
    var $results = array();  //array of query results by index
55
    var $num_rows = array(); //array of row numbers by index
56
    var $queries = array();  //array of query strings by index
57
58
    var $error_callback;   // function for displaying fatal errors
59
    var $warning_callback// function for displaying warnings
60
61
    var $pconnect false// use persistent connections?
62
63
    /*
64
    ** Return version string.
65
    */
66
    function version()
67
    {
68
        return("2.0.0");
69
    }
70
71
    /*
72
    ** Return L1 SQL API version
73
    */
74
    function apiversion()
75
    {
76
        return("2.0");
77
    }
78
79
    /*
80
    ** For getting member variable values
81
    */
82
    function get($var)
83
    {
84
        return($this->$var);
85
    }
86
87
    /*
88
    ** For setting member variable values
89
    */
90
    function set($var,$value)
91
    {
92
        $this->$var $value;
93
        return($value);
94
    }
95
96
    /*
97
    ** Escape and prepare values for inclusion in a MySQL query.
98
    **
99
    ** Backslashes single slashes and adds single slashes around values.
100
    ** eg: "O'Reilly's" => "'O\'Reilly\'s'"
101
    ** Also converts php null value to "null"
102
    **
103
    ** If data is an array (must be associative), the keys are treated as
104
    ** column names and the values are treated as SQL values and both are
105
    ** escaped as such.
106
    **
107
    ** The $use_quotes argument dictates whether to enclose values in single
108
    ** quotes.
109
    */
110
    function prepare_value($data$use_quotes TRUE)
111
    {
112
        if($use_quotes)
113
            $quote "'";
114
        else
115
            $quote "";
116
117
        if(is_array($data))
118
        {
119
            $a_sql = array();
120
            foreach($data as $field => $value)
121
            {
122
                $a_sql$this->prepare_value($field,FALSE) ] = ($value === null "null" $quote.utf8_encode(mysql_real_escape_string($value,$this->session)).$quote);
123
            }
124
            return($a_sql);
125
        } else {
126
            $sql = ($data === null "null" $quote.utf8_encode(mysql_real_escape_string($data,$this->session)).$quote);
127
            return($sql);
128
        }
129
    }
130
131
    /*
132
    ** Format an array of data into an INSERT query
133
    ** Data is in the form of array(columname=>value,...)
134
    **
135
    ** It will pass the query to issue_query() unless the third parameter is TRUE,
136
    ** in which case it returns the SQL statement generated.
137
    **
138
    ** Otherwise it returns the index of the query or FALSE if the query failed.
139
    */
140
    function insert($table,$a_values,$return false)
141
    {
142
        if(!is_array($a_values))
143
        {
144
            $this->error("Argument to L1_MySQL::insert() must be an array.");
145
        }
146
    
147
        if(!$this->set && !stristr($table,"."))
148
        {
149
            if(!$this->trycode("database"))
150
                $this->error("Could not change database, database not specified in table parameter");
151
        }
152
153
        $a_sql $this->prepare_value($a_values);
154
    
155
        $fields implode(",",array_keys($a_sql));
156
        $values implode(",",array_values($a_sql));
157
158
        $sql "INSERT INTO $table ($fields) VALUES($values)";
159
        if($return)
160
            return($sql);
161
        $index $this->trycode("issue_query",$sql);
162
        if(!$index)
163
            $this->error("Error issuing INSERT query.");
164
        return($index);
165
    }
166
167
    /*
168
    ** Format an array of data into an UPDATE query
169
    ** Data is in the form of array(columname=>value,...)
170
    **
171
    ** The $condition argument is an array in the form of
172
    ** array(columnname=>value), and it causes the statement to be applied where
173
    ** columnname has a value equal to value.
174
    **
175
    ** It will pass the query to issue_query() unless the fourth parameter is
176
    ** TRUE, in which case it returns the SQL statement generated.
177
    **
178
    ** Otherwise it returns the index of the query or FALSE if the query failed.
179
    */
180
    function update($table,$a_values,$condition,$return false)
181
    {
182
        if(!$this->set && !stristr($table,"."))
183
        {
184
            if(!$this->trycode("database"))
185
                $this->error("Could not change database, database not specified in table parameter");
186
        }
187
188
        $a_sql = array();
189
        $a_sql $this->prepare_value($a_values);
190
        $condition $this->prepare_value($condition);
191
        foreach($a_sql as $col => $val)
192
            $set .= "$col = $val, ";
193
        $set substr($set,0,-2);
194
195
        foreach($condition as $col => $val)
196
            $cond .= "$col = $val AND ";
197
        $cond substr($cond,0,-5);
198
199
        $sql "UPDATE $table SET $set WHERE $cond";
200
        if($return)
201
            return($sql);
202
        $index $this->trycode('issue_query',$sql);
203
        if(!$index)
204
            $this->error("Error issuing UPDATE query.");
205
        return($index);
206
    }
207
208
    /*
209
    ** Issue a mysql query.
210
    **
211
    ** Returns the array index that will be used to store results and rows,
212
    ** returns FALSE on query failure
213
    */
214
    function issue_query($query)
215
    {
216
        list($usec,$sec) = explode(" ",microtime()); // this may be changed in the future
217
        $index = (string) $sec substr($usec,1);    //
218
        if(!@mysql_get_server_info($this->session))
219
        {
220
            if(!$this->trycode("connect")) // try to connect with current params
221
            {
222
                $this->error("Driver not connected, connection tried and failed.");
223
                return(false);
224
            }
225
        }
226
        $this->queries[$index] = $query;
227
        $this->num_rows[$index] = 0;
228
        $this->results[$index] = @mysql_query($query,$this->session);
229
        // hmm, should we generate error or leave it up to scripts? For now, yes.
230
        if($this->check_error("Query error",$php_errormsg)) return(false);
231
        if(!$this->results[$index])
232
        {
233
            $this->error("Query error");
234
            return(false);
235
        }
236
        $this->num_rows[$index] = @mysql_affected_rows($this->session);
237
        return $index;
238
    }
239
240
    /*
241
    ** Returns an array of all the tables in the current database as a numeric
242
    ** array
243
    **
244
    ** Returns FALSE on any kind of error.
245
    */
246
    function fetch_tables()
247
    {
248
        if(!$this->set)
249
            $this->error("Database connection is not set.");
250
251
        list($usec,$sec) = explode(" ",microtime()); // same method as issue_query()
252
        $index = (string) $sec substr($usec,1);    //
253
254
        $this->results[$index] = @mysql_list_tables($this->database,$this->session);
255
        $this->queries[$index] = "L1_MySQL::fetch_tables()"// special query
256
        $this->num_rows[$index] = 0;
257
258
        if($this->check_error("Error listing tables")) return(false);
259
        if(!$this->results[$index])
260
        {
261
            $this->error("Error listing tables",$php_errormsg);
262
            return(false);
263
        }
264
265
        $this->num_rows[$index] = @mysql_affected_rows($this->session);
266
267
        $return = array();
268
        while($row = @mysql_fetch_array($this->results[$index],L1SQL_NUM))
269
        {
270
            if($this->check_error("Error fetching table listing")) return(false);
271
            $return array_merge($return,$row);
272
        }
273
        return($return);
274
    }
275
276
    /*
277
    ** Fetch all the rows of a query
278
    **
279
    ** This will return a multidimensional array of the form
280
    ** $return[row][column]
281
    ** You must give the index returned by issue_query.
282
    ** Specify the format of the column as the first argument (it is a constant
283
    ** passed to mysql_fetch_array ;) and if you want the rows indexed by a
284
    ** particular field, list that as the second argument, otherwise they'll be
285
    ** numeric in the order returned by MySQL.
286
    ** The results are also stored in $this->row[$index]
287
    */
288
    function fetch_all($index$type L1SQL_BOTH$index_by "")
289
    {
290
        if(($type != L1SQL_ASSOC) && ($type != L1SQL_NUM) && ($type != L1SQL_BOTH))
291
        {
292
            $this->error("Invalid type specified for L1_MySQL::fetch_all");
293
            return(false);
294
        }
295
        $return = array();
296
        for($i=0;$row = @mysql_fetch_array($this->results[$index],$type);$i++)
297
        {
298
            if($index_by !== "")
299
            {
300
                $field $row[$index_by];
301
                $return[$field] = $row;
302
            } else {
303
                $return[$i] = $row;
304
            }
305
        }
306
        //$this->free_result($index);
307
        return($return);
308
    }
309
310
    /*
311
    ** Fetch the result as an array of rows in a column
312
    **
313
    ** For use when the result is many rows containing one column each
314
    ** It will return a numeric array based on the order the rows are returned.
315
    */
316
    function fetch_column($index$col_offset 0$index_by "")
317
    {
318
        $return=array();
319
        for($i=0;$row = @mysql_fetch_array($this->results[$index],L1SQL_BOTH);$i++)
320
        {
321
            $this->check_error("Error fetching column",$php_errormsg);
322
            $php_errormsg false;
323
            if(!isset($row[$col_offset]))
324
            {
325
                $this->error("Column offset too high: column $col_offset requested, ".
326
                    count($row)." columns in result");
327
                return(false);
328
            }
329
            $idx $index_by === "" $i $row[$index_by];
330
            $return[$idx]=$row[$col_offset];
331
            //$this->free_result($index);
332
        }
333
        return($return);
334
    }
335
336
    /*
337
    ** Fetch one row of the result of a query.
338
    **
339
    ** You must give the index returned by issue_query().
340
    ** The return value can be an associative array (column => value),
341
    ** numeric ([0] => value), or both.
342
    ** Specify the argument to mysql_fetch_array.
343
    */
344
    function fetch_row($index$row_offset 0$type L1SQL_BOTH)
345
    {
346
        if(($type != L1SQL_ASSOC) && ($type != L1SQL_NUM) && ($type != L1SQL_BOTH))
347
        {
348
            $this->error("Invalid type specified for L1_MySQL::fetch_all");
349
            return(false);
350
        }
351
352
        if($this->num_rows[$index] < $row_offset)
353
        {
354
            $this->error("Row offset too high: row $row_offset requested, ".
355
                $this->num_rows[$index]." rows in result.");
356
            return(false);
357
        }
358
359
        // loop through specified # of rows
360
        for($i=0;$i<$row_offset;$i++)
361
            @mysql_fetch_array($this->results[$index]);
362
363
        $row = @mysql_fetch_array($this->results[$index],$type);
364
        if($this->check_error("Error fetching row",$php_errormsg)) return(false);
365
        if(is_array($row))
366
        {
367
            return($row);
368
        } else {
369
            //$this->free_result($index);
370
            return(false);
371
        }
372
    }
373
374
    /*
375
    ** Fetch a single variable from a query
376
    **
377
    ** You must give the index returned by issue_query().
378
    **
379
    ** Use this to get a single value from a query at the specified row and column
380
    ** offsets. The column offset can be a string row name or numeral row number.
381
    **
382
    ** On error, a message is generated and the function returns FALSE.
383
    */
384
    function fetch_var($index$row_offset 0$col_offset 0)
385
    {
386
        if($this->num_rows[$index] < $row_offset)
387
        {
388
            $this->error("Row offset too high: row $row_offset requested, ".
389
                "{$this->num_rows[$index]} rows in result");
390
            return(false);
391
        }
392
393
        // loop through the specified # of rows
394
        for($i=0;$i<$row_offset;$i++)
395
            @mysql_fetch_array($this->results[$index]);
396
397
        $row = @mysql_fetch_array($this->results[$index],L1SQL_NUM);
398
        if($this->check_error("Error fetching row",$php_errormsg)) return(false);
399
400
        //if(isset($row[$col_offset]))
401
        if(in_array($col_offsetarray_keys($row)))
402
        {
403
            $return $row[$col_offset];
404
        } else {
405
            $this->error("Column offset too high: column $col_offset requested, ".
406
                count($row)." columns in result");
407
            $return false;
408
        }
409
410
        return($return);
411
    }
412
413
    /*
414
    ** Free result of a query
415
    **
416
    ** You must give the index of the results to free
417
    ** This function isn't actually ever used here; all results are saved.
418
    ** You can use this if you want, but it isn't really necessary.
419
    */
420
    function free_result($index)
421
    {
422
        if(@is_resource($this->results[$index]))
423
        {
424
            $ok = @mysql_free_result($this->results[$index]);
425
            if(!$ok)
426
                $this->error("Error freeing result",$php_errormsg);
427
        } else {
428
            $this->error("Result is not a resource",$php_errormsg);
429
        }
430
        unset($this->results[$index]);
431
        // might as well unset these too
432
        unset($this->query[$index]);
433
        unset($this->num_rows[$index]);
434
435
        return(true);
436
    }
437
438
    /*
439
    ** Print an error
440
    **
441
    ** This is the origional error message function that only prints one line of
442
    ** backtrace info (it tries to determine the most relevant line)
443
    */
444
    function errorMsg($message,$backtrace,$severity)
445
    {
446
        if(empty($backtrace))
447
        {
448
            $out "$message<br /><i>no backtrace info available</i><br />\n";
449
        } else {
450
            for($i=count($backtrace);$i>-1;$i--)
451
            {
452
                if(!stristr($backtrace[$i]['file'],__FILE__))
453
                {
454
                    $traceline $backtrace[$i+1];
455
                }
456
            }
457
458
            $line = isset($traceline['line'])?$traceline['line']:"unknown";
459
            $file = isset($traceline['file'])?$traceline['file']:"unknown";
460
            $function = isset($traceline['function'])?$traceline['function']."()":"unknown";
461
            $class = isset($traceline['class'])? " in class <b>".$traceline['class']."</b>" "";
462
            $out "$message<br />in fule <b>$file</b> on line <b>$line</b> in function <b>$function</b>$class<br />";
463
        }
464
        switch($severity)
465
        {
466
case E_USER_ERROR:
467
            if($this->error_callback === FALSE)
468
                ; // do nothing
469
            elseif($this->error_callback !== null)
470
                call_user_func($this->error_callback,$out);
471
            else
472
                print($out);
473
            exit;
474
475
case E_USER_WARNING:
476
            if($this->warning_callback === FALSE)
477
                ; // do nothing
478
            elseif($this->warning_callback !== null)
479
                call_user_func($this->warning_callback,$out);
480
            else
481
                print($out);
482
483
case E_USER_NOTICE:
484
default:
485
            break;
486
        }
487
    }
488
489
    function multiErrorMsg($message,$backtrace,$severity)
490
    {
491
        switch($severity)
492
        {
493
case E_USER_ERROR:
494
case E_USER_WARNING:
495
                $out $message."<br />\n";
496
                if(empty($backtrace))
497
                {
498
                    $out .= "<i>no backtrace info available</i><br />\n";
499
                } else {
500
                    foreach($backtrace as $entry)
501
                    {
502
                        $line = isset($entry['line']) ? $entry['line'] : "unknown";
503
                        $file = isset($entry['file']) ? $entry['file'] : "unknown";
504
                        $function = isset($entry['function']) ? $entry['function']."()" "unknown";
505
                        $class = isset($entry['class']) ? " in class <b>".$entry['class']."</b>" "";
506
                        $out .= "in file <b>".$file."</b> on line <b>".$line."</b> in function <b>".$function."</b>".$class."<br />\n";
507
                    }
508
                }
509
                if($severity == E_USER_ERROR)
510
                {
511
                    if($this->warning_callback === FALSE) {
512
                        // do nothing
513
                    } elseif($this->error_callback !== null) {
514
                        call_user_func($this->error_callback,$out."<b>FATAL</b>\n");
515
                        exit;
516
                    } else {
517
                        print($out."<b>FATAL</b>\n");
518
                        exit;
519
                    }
520
                } else {
521
                    if($this->warning_callback === FALSE) {
522
                        // do nothing
523
                    } elseif($this->warning_callback !== null) {
524
                        call_user_func($this->warning_callback,$out);
525
                    } else {
526
                        print($out);
527
                    }
528
                }
529
530
case E_USER_NOTICE:
531
default:
532
                break;
533
        }
534
    }
535
536
    /*
537
    ** Handle an error
538
    **
539
    ** Use this when you're sure there is an error, and you want
540
    ** it cleanly handled.
541
    ** It will display the error code and explanation as well as
542
    ** the message you set.
543
    **
544
    ** You MUST pass $php_errormsg as the second parameter for it to be used!
545
    **
546
    ** It will obey $halt_on_error unless behavior
547
    ** is otherwise specified in the third parameter.
548
    **
549
    ** The fourth parameter should only be set when being called by check_error().
550
    **
551
    ** If the fifth parameter is true, a shorter error message is printed
552
    ** (with only one line of backtrace instead of the default all).
553
    */
554
    function error($message ""$php ""$force_abort ""$check_error ""$short_error false)
555
    {
556
        if($check_error === ""// we're being called from a script
557
        {
558
            if(mysql_errno()) //MySQL error
559
            {
560
                $type "MySQL";
561
                $this->error_no mysql_errno();
562
                $this->error_str mysql_error();
563
            } elseif($php) { //PHP error
564
                $type "Script";
565
                $this->error_no = -1;
566
                $this->error_str $php;
567
            } else { //some other error
568
                $type "Special";
569
                $this->error_no = -1;
570
                $this->error_str "L1 MySQL Driver special error";
571
            }
572
        } else { // check_error() has already diagnosed the problem
573
            $type $check_error;
574
        }
575
576
        $msg_func = ($short_error) ? "errorMsg" "multiErrorMsg";
577
578
        if(function_exists("debug_backtrace"))
579
            $backtrace debug_backtrace();
580
        else
581
            $backtrace null;
582
583
        if($force_abort !== "" $force_abort $this->halt_on_error)
584
        {
585
            $this->disconnect();
586
            $this->$msg_func(nl2br(htmlspecialchars(
587
                "$message\n$type error #".$this->error_no." (".$this->error_str.")"
588
            )),$backtrace,E_USER_ERROR);
589
        } else {
590
            $this->$msg_func(nl2br(htmlspecialchars(
591
                "$message\n$type warning #".$this->error_no." (".$this->error_str.")"
592
            )),$backtrace,E_USER_WARNING);
593
        }
594
    }
595
596
    /*
597
    ** Check for an error
598
    **
599
    ** This is for use when you aren't sure there's an error
600
    ** or it's not very easy to test for it. This will catch it.
601
    **
602
    ** You MUST pass $php_errormsg as the second parameter for it to be used!
603
    **
604
    ** Set the message to give if an error is found,
605
    ** and if it's necessary to stop on error, set the third parameter.
606
    **
607
    ** This function returns TRUE if an error was detected, otherwise FALSE;
608
    ** this makes it easy to do things like:
609
    **   if(check_error("message")) return(false);
610
    */
611
    function check_error($message ""$php ""$force_abort ""$short_error false)
612
    {
613
        if(mysql_errno()) //mysql error
614
        {
615
            $type "MySQL";
616
            $this->error_no mysql_errno();
617
            $this->error_str mysql_error();
618
        } elseif($php) { //php error
619
            $type "Script";
620
            $this->error_no = -1;
621
            $this->error_str $php;
622
        } else { //no error
623
            $type "";
624
            $this->error_no 0;
625
            $this->error_str "";
626
        }
627
628
        if($type != "")
629
        {
630
            if($message !== "")
631
            {
632
                $this->error($message,$php,$force_abort,$type,$short_error);
633
                return(true);
634
            } else {
635
                return(true);
636
            }
637
        } else {
638
            $php_errormsg null//clear any errors generated by this function.
639
            return(false); //all quiet on the western front ;)
640
        }
641
    }
642
643
    /*
644
    ** Switch to the selected database
645
    */
646
    function database($database)
647
    {
648
        if(!$this->session)
649
        {
650
            $conn_success $this->trycode("connect");
651
            if(!$conn_success)
652
            {
653
                $this->error("Could not connect");
654
                return(false);
655
            }
656
        }
657
658
        $ok = @mysql_select_db($database$this->session);
659
        if(!$ok)
660
        {
661
            $this->error("Error selecting database",$php_errormsg);
662
            return(false);
663
        }
664
665
        $this->database $database;
666
        $this->set true;
667
        return(true);
668
    }
669
670
    /*
671
    ** Connect to database server
672
    **
673
    ** It will use given connection settings or use ones already in the class.
674
    ** It returns true on success.
675
    ** Host should be either "localhost[:/path/to/sock]", "1.2.3.4[:port]" or
676
    **   "hostname[:port]". Only use "localhost..." for local socket connections.
677
    */
678
    function connect($hostname ""$username ""$password "")
679
    {
680
        if($hostname === "")
681
            $hostname $this->hostname;
682
        else
683
            $this->hostname $hostname;
684
685
        if($username === "")
686
            $username $this->username;
687
        else
688
            $this->username $username;
689
690
        if($password === "")
691
            $password $this->password;
692
        else
693
            $this->password $password;
694
695
        if($this->pconnect)
696
        {
697
            $this->session = @mysql_pconnect($hostname,$username,$password);
698
        } else {
699
            $this->session = @mysql_connect($hostname,$username,$password);
700
        }
701
702
        if(!$this->session)
703
        {
704
            $this->error("Could not connect to database server",$php_errormsg);
705
            return(false);
706
        } else {
707
            return(true);
708
        }
709
    }
710
711
    /*
712
    ** Disconnect from database
713
    ** and optionally reset the class
714
    */
715
    function disconnect($reset false)
716
    {
717
        if(!@mysql_get_server_info($this->session))
718
            return(false);
719
        $ok = @mysql_close($this->session);
720
        if(!$ok)
721
        {
722
            $this->error("Couldn't disconnect from database server",$php_errormsg);
723
            return(false);
724
        }
725
        $this->session "";
726
        $this->set false;
727
        if($reset)
728
        {
729
            $this->hostname "";
730
            $this->username "";
731
            $this->password "";
732
            $this->database "";
733
734
            $this->error_no 0;
735
            $this->error_str "";
736
737
            $this->results = array();
738
            $this->num_rows = array();
739
            $this->queries = array();
740
741
            $this->error_callback null;
742
            $this->warning_callback null;
743
            $this->pconnect false;
744
        }
745
        return(true);
746
    }
747
748
    /*
749
    ** Instantiater function
750
    **
751
    ** If given the proper values, will set up class and connect to database server.
752
    ** If also given a database, will switch to the database.
753
    */
754
    function L1_MySQL($hostname ""$username ""$password ""$database ""$halt_on_error true$pconnect false)
755
    {
756
        $this->halt_on_error $halt_on_error//override the default
757
        $this->pconnect $pconnect// override the default
758
        if(($hostname !== "") && ($username !== "") && ($password !== ""))
759
        {
760
            $this->session = @mysql_connect($hostname,$username,$password);
761
            if(!@mysql_get_server_info($this->session))
762
                $this->error("Could not connect to database server",$php_errormsg);
763
            $this->hostname $hostname;
764
            $this->username $username;
765
            $this->password $password;
766
767
            if($database !== ""//just to be safe; database could be "false" or "0"
768
            {
769
                $this->database $database;
770
                $ok = @mysql_select_db($database,$this->session);
771
                if(!$ok)
772
                    $this->error("Could not select database",$php_errormsg);
773
                $this->set true;
774
            } else {
775
                $this->set false;
776
            }
777
        }
778
    }
779
780
    /*
781
    ** Class dumper
782
    **
783
    ** This is a good function to use for debugging
784
    ** WARNING!! This will reveal the password field of the class unless
785
    ** you specify otherwise!
786
    */
787
    function dump($no_password false)
788
    {
789
        if(!$no_password)
790
        {
791
            print("<pre>\n");
792
            print_r($this);
793
            print("</pre>\n");
794
        } else {
795
            ob_start();
796
            print("<pre>\n");
797
            print_r($this);
798
            print("</pre>\n");
799
            $out ob_get_contents();
800
            ob_clean();
801
            $out ereg_replace("    \[password\] => [^\n]*\n","    [password] => *****\n",$out);
802
            print($out);
803
        }
804
    }
805
806
    /*
807
    ** Executes code in an environment where errors will be completely ignored.
808
    ** Error messages will be suppressed, and execution will continue thru errors.
809
    **
810
    ** To execute code, pass the name of the function in this class to execute,
811
    ** and pass the remaining arguments in order.
812
    */
813
    function trycode($func)
814
    {
815
        ob_start();
816
        $params func_get_args();
817
        array_shift($params);
818
819
        $old_halt $this->halt_on_error;
820
        $this->halt_on_error false;
821
        $old_warning_callback $this->warning_callback;
822
        $this->warning_callback null;
823
        $old_error_callback $this->error_callback;
824
        $this->error_callback null;
825
826
        $retval call_user_func_array(array(&$this,$func),$params);
827
        //eval('$retval = '.$eval_code);
828
829
        $this->error_callback $old_error_callback;
830
        $this->warning_callback $old_warning_callback;
831
        $this->halt_on_error $old_halt;
832
833
        ob_end_clean();
834
        return($retval);
835
    }
836
837
    function register_error_func($callback,$type "")
838
    {
839
        switch($type)
840
        {
841
case E_USER_ERROR:
842
            if(function_exists($callback))
843
                $this->error_callback $callback;
844
            break;
845
846
case E_USER_WARNING:
847
            if(function_exists($callback))
848
                $this->warning_callback $callback;
849
            break;
850
851
default:        if(function_exists($callback))
852
                $this->error_callback $this->warning_callback $callback;
853
            break;
854
        }
855
    }
856
857
    function unregister_error_func($type "")
858
    {
859
        switch($type)
860
        {
861
case E_USER_ERROR:
862
            $this->error_callback null;
863
            break;
864
865
case E_USER_WARNING:
866
            $this->warning_callback null;
867
            break;
868
869
default:        $this->error_callback $this->warning_callback null;
870
            break;
871
        }
872
    }
873
874
}}
875
876
/*
877
** Simple replacement function for pre PHP 4.3.0
878
*/
879
if(!function_exists("mysql_real_escape_string"))
880
{
881
    function mysql_real_escape_string($string$link "")
882
    {
883
        $search = array
884
        (
885
            "\\",
886
            "\x00",
887
            "\n",
888
            "\r",
889
            "'",
890
            "\"",
891
            "\x1a",
892
        );
893
        $replace = array
894
        (
895
            "\\\\",
896
            "\\\x00",
897
            "\\\n",
898
            "\\\r",
899
            "\\'",
900
            "\\\"",
901
            "\\\x1a",
902
        );
903
        return(str_replace($search,$replace,$string));
904
    }
905
}
906
907
?>
908

powered by Codewise Manager v0.1-DEV :: 180.77ms, 6 ops, 3 queries