Commit 00529ef3 authored by Alexander Butenko's avatar Alexander Butenko

Rework constructor to allow charset modification

parent 6d29fa7e
......@@ -92,6 +92,7 @@ class MysqliDb
protected $password;
protected $db;
protected $port;
protected $charset;
/**
* Is Subquery object
......@@ -106,24 +107,36 @@ class MysqliDb
* @param string $db
* @param int $port
*/
public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL, $port = NULL)
public function __construct($host = NULL, $username = NULL, $password = NULL, $db = NULL, $port = NULL, $charset = 'utf8')
{
$isSubQuery = false;
// if params were passed as array
if (is_array ($host)) {
foreach ($host as $key => $val)
$$key = $val;
}
// if host were set as mysqli socket
if (is_object ($host))
$this->_mysqli = $host;
else
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
if($port == NULL)
$this->port = ini_get ('mysqli.default_port');
else
$this->port = $port;
$this->charset = $charset;
if ($username == null && $db == null) {
if ($isSubQuery) {
$this->isSubQuery = true;
return;
}
// for subqueries we do not need database connection and redefine root instance
if (!is_object ($host))
$this->connect();
$this->setPrefix();
self::$_instance = $this;
}
......@@ -137,10 +150,14 @@ class MysqliDb
if ($this->isSubQuery)
return;
if (empty ($this->host))
die ('Mysql host is not set');
$this->_mysqli = new mysqli ($this->host, $this->username, $this->password, $this->db, $this->port)
or die('There was a problem connecting to the database');
$this->_mysqli->set_charset ('utf8');
if ($this->charset)
$this->_mysqli->set_charset ($this->charset);
}
/**
* A method of returning the static instance to allow access to the
......@@ -1044,7 +1061,7 @@ class MysqliDb
*/
public static function subQuery($subQueryAlias = "")
{
return new MysqliDb ($subQueryAlias);
return new MysqliDb (Array('host' => $subQueryAlias, 'isSubQuery' => true));
}
/**
......
......@@ -25,12 +25,29 @@ To utilize this class, first import MysqliDb.php into your project, and require
require_once ('MysqliDb.php');
```
After that, create a new instance of the class.
Simple initialization with utf8 charset by default:
```php
$db = new MysqliDb ('host', 'username', 'password', 'databaseName');
```
Advanced initialization. If no charset should be set charset, set it to null
```php
$db = new Mysqlidb (Array (
'host' => 'host',
'username' => 'username',
'password' => 'password',
'db'=> 'databaseName',
'port' => 3306,
'charset' => 'utf8'));
```
port and charset params are optional.
Reuse already connected mysqli:
```php
$mysqli = new mysqli ('host', 'username', 'password', 'databaseName');
$db = new Mysqlidb ($mysqli);
```
Its also possible to set a table prefix:
```php
$db->setPrefix ('my_');
......
......@@ -5,6 +5,17 @@ error_reporting(E_ALL);
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
if(!$db) die("Database error");
$db = new Mysqlidb(Array (
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db'=> 'testdb',
'charset' => null));
if(!$db) die("Database error");
$mysqli = new mysqli ('localhost', 'root', '', 'testdb');
$db = new Mysqlidb($mysqli);
$prefix = 't_';
$db->setPrefix($prefix);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment