@@ -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 /**
0 commit comments