Project

General

Profile

« Previous | Next » 

Revision 6

Added by Manuela over 6 years ago

modified class database to default charset utf8mb4
added property database->sTablePrefix

View differences:

class.database.php
36 36

  
37 37
    private $db_handle  = null; // readonly from outside
38 38
    private $db_name    = '';
39
    private $sTablePrefix = '';
39 40
    private $connected  = false;
40
    private $sCharset   = '';
41
    private $sCharset   = 'utf8mb4';
42
    private $sCollation = 'utf8mb4_unicode_ci';
41 43
    private $error      = '';
42 44
    private $error_no   = array();
43 45
    private $error_type = '';
......
46 48

  
47 49

  
48 50
    // Set DB_URL
49
    function __construct($url = '') {
51
    function __construct($url = '')
52
    {
50 53
        // Connect to database
51 54
        if (!$this->connect()) {
52 55
            throw new DatabaseException($this->get_error());
53 56
        }
57
        $this->sTablePrefix = TABLE_PREFIX;
54 58
    }
55 59

  
56 60
    // Connect to the database   DB_CHARSET
57
    function connect() {
58

  
59
        $this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', (defined('DB_CHARSET') ? DB_CHARSET : '')));
60

  
61
        if (defined('DB_PORT')) {
62
            $port = DB_PORT;
63
        } else {
64
            $port = ini_get('mysqli.default_port');
65
        }
61
    function connect()
62
    {
63
        $aTmp = preg_split(
64
            '/[^a-z0-9]/i',
65
            strtolower(preg_replace('/[^a-z0-9_]/i', '', (defined('DB_CHARSET') ? DB_CHARSET : 'utf8mb4_unicode_ci')))
66
        );
67
        $this->sCharset = $aTmp[0];
68
        $this->sCollation = implode('_', $aTmp);
69
        $port = defined('DB_PORT') ? DB_PORT : ini_get('mysqli.default_port');
66 70
        if (!($this->db_handle = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, $port))) {
67 71
            $this->connected = false;
68 72
            $this->error = mysqli_connect_error();
69 73
        } else {
70 74
            if ($this->sCharset) {
71
                @mysqli_query($this->db_handle, 'SET NAMES '.$this->sCharset);
75
                mysqli_query($this->db_handle, 'SET NAMES '.$this->sCharset);
72 76
                mysqli_set_charset($this->db_handle, $this->sCharset);
73 77
            }
74 78
            $this->db_name = DB_NAME;
......
78 82
    }
79 83

  
80 84
    // Disconnect from the database
81
    function disconnect() {
85
    function disconnect()
86
    {
82 87
        if($this->connected==true) {
83 88
            mysqli_close();
84 89
            return true;
......
88 93
    }
89 94

  
90 95
    // Run a query
91
    function query($statement) {
96
    function query($statement)
97
    {
92 98
        $mysql = new mysql($this->db_handle);
93 99
        $mysql->query($statement);
94 100
        $this->set_error($mysql->error());
......
114 120
    }
115 121

  
116 122
    // Set the DB error
117
    function set_error($message = null) {
123
    function set_error($message = null)
124
    {
118 125
        $this->error = $message;
119 126
        $this->error_type = 'unknown';
120 127
        if ($message!=''){
......
122 129
    }
123 130

  
124 131
    // Return true if there was an error
125
    function is_error() {
132
    function is_error()
133
    {
126 134
        return (!empty($this->error)) ? true : false;
127 135
    }
128 136

  
129 137
    // Return the error
130
    function get_error() {
138
    function get_error()
139
    {
131 140
        return $this->error;
132 141
    }
133 142
    // Return the errno
134
    function get_errno() {
143
    function get_errno()
144
    {
135 145
        return $this->is_error() ? mysqli_errno($this->db_handle) : 0;
136 146
    }
137 147
/**
......
150 160
            case 'DbName':
151 161
                $retval = $this->db_name;
152 162
                break;
163
            case 'sTablePrefix':
164
            case 'TablePrefix':
165
                $retval = $this->sTablePrefix;
166
                break;
153 167
            default:
154 168
                $retval = null;
155 169
                break;
......
347 361
        $sTablePrefix  = '',
348 362
        $mAction       = true,
349 363
        $sTblEngine    = 'MyISAM',
350
        $sTblCollation = 'utf8_unicode_ci'
364
        $sTblCollation = 'utf8mb4_unicode_ci'
351 365
    ) {
352 366
        $iCount = 0;
353 367
        $sSqlBuffer  = '';
......
477 491
define('MYSQLI_SEEK_FIRST', 0);
478 492
define('MYSQLI_SEEK_LAST', -1);
479 493

  
480
class mysql {
481

  
494
class mysql
495
{
482 496
    private $db_handle = null;
483 497
    private $result = null;
484 498
    private $error = '';
485 499

  
486
    public function __construct($handle) {
500
    public function __construct($handle)
501
    {
487 502
        $this->db_handle = $handle;
488 503
    }
489 504
/**
......
507 522
    }
508 523

  
509 524
    // Fetch num rows
510
    public function numRows() {
525
    public function numRows()
526
    {
511 527
        return mysqli_num_rows($this->result);
512 528
    }
513 529

  
514 530
    // Fetch row  $typ = MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
515
    public function fetchRow($typ = MYSQLI_BOTH) {
531
    public function fetchRow($typ = MYSQLI_BOTH)
532
    {
516 533
        return mysqli_fetch_array($this->result, $typ);
517 534
    }
518 535
/**
......
586 603
    }
587 604

  
588 605
    // Get error
589
    public function error() {
590
        if(isset($this->error)) {
606
    public function error()
607
    {
608
        if (isset($this->error)) {
591 609
            return $this->error;
592 610
        } else {
593 611
            return null;

Also available in: Unified diff