Skip to content

Commit 00529ef

Browse files
committed
Rework constructor to allow charset modification
1 parent 6d29fa7 commit 00529ef

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

MysqliDb.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class MysqliDb
9292
protected $password;
9393
protected $db;
9494
protected $port;
95+
protected $charset;
9596

9697
/**
9798
* Is Subquery object
@@ -106,24 +107,36 @@ class MysqliDb
106107
* @param string $db
107108
* @param int $port
108109
*/
109-
public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL, $port = NULL)
110+
public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL, $port = NULL, $charset = 'utf8')
110111
{
111-
$this->host = $host;
112+
$isSubQuery = false;
113+
114+
// if params were passed as array
115+
if (is_array ($host)) {
116+
foreach ($host as $key => $val)
117+
$$key = $val;
118+
}
119+
// if host were set as mysqli socket
120+
if (is_object ($host))
121+
$this->_mysqli = $host;
122+
else
123+
$this->host = $host;
124+
112125
$this->username = $username;
113126
$this->password = $password;
114127
$this->db = $db;
115-
if($port == NULL)
116-
$this->port = ini_get ('mysqli.default_port');
117-
else
118-
$this->port = $port;
128+
$this->port = $port;
129+
$this->charset = $charset;
119130

120-
if ($username == null && $db == null) {
131+
if ($isSubQuery) {
121132
$this->isSubQuery = true;
122133
return;
123134
}
124135

125136
// for subqueries we do not need database connection and redefine root instance
126-
$this->connect();
137+
if (!is_object ($host))
138+
$this->connect();
139+
127140
$this->setPrefix();
128141
self::$_instance = $this;
129142
}
@@ -137,10 +150,14 @@ public function connect()
137150
if ($this->isSubQuery)
138151
return;
139152

153+
if (empty ($this->host))
154+
die ('Mysql host is not set');
155+
140156
$this->_mysqli = new mysqli ($this->host, $this->username, $this->password, $this->db, $this->port)
141157
or die('There was a problem connecting to the database');
142158

143-
$this->_mysqli->set_charset ('utf8');
159+
if ($this->charset)
160+
$this->_mysqli->set_charset ($this->charset);
144161
}
145162
/**
146163
* A method of returning the static instance to allow access to the
@@ -1044,7 +1061,7 @@ public function func ($expr, $bindParams = null) {
10441061
*/
10451062
public static function subQuery($subQueryAlias = "")
10461063
{
1047-
return new MysqliDb ($subQueryAlias);
1064+
return new MysqliDb (Array('host' => $subQueryAlias, 'isSubQuery' => true));
10481065
}
10491066

10501067
/**

readme.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,29 @@ To utilize this class, first import MysqliDb.php into your project, and require
2525
require_once ('MysqliDb.php');
2626
```
2727

28-
After that, create a new instance of the class.
29-
28+
Simple initialization with utf8 charset by default:
3029
```php
3130
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');
3231
```
3332

33+
Advanced initialization. If no charset should be set charset, set it to null
34+
```php
35+
$db = new Mysqlidb (Array (
36+
'host' => 'host',
37+
'username' => 'username',
38+
'password' => 'password',
39+
'db'=> 'databaseName',
40+
'port' => 3306,
41+
'charset' => 'utf8'));
42+
```
43+
port and charset params are optional.
44+
45+
Reuse already connected mysqli:
46+
```php
47+
$mysqli = new mysqli ('host', 'username', 'password', 'databaseName');
48+
$db = new Mysqlidb ($mysqli);
49+
```
50+
3451
Its also possible to set a table prefix:
3552
```php
3653
$db->setPrefix ('my_');

tests.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
66
if(!$db) die("Database error");
77

8+
$db = new Mysqlidb(Array (
9+
'host' => 'localhost',
10+
'username' => 'root',
11+
'password' => '',
12+
'db'=> 'testdb',
13+
'charset' => null));
14+
if(!$db) die("Database error");
15+
16+
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
17+
$db = new Mysqlidb($mysqli);
18+
819
$prefix = 't_';
920
$db->setPrefix($prefix);
1021

0 commit comments

Comments
 (0)