Merge pull request #2758 from annando/1609-sql-charset

Set the charset for the SQL connection for new installations
This commit is contained in:
Tobias Diekershoff
2016-09-01 09:22:07 +02:00
committed by GitHub
6 changed files with 91 additions and 59 deletions

View File

@@ -66,6 +66,8 @@ class dba {
if(! mysqli_connect_errno()) {
$this->connected = true;
}
if (isset($a->config["system"]["db_charset"]))
$this->db->set_charset($a->config["system"]["db_charset"]);
}
else {
$this->mysqli = false;
@@ -73,6 +75,8 @@ class dba {
if($this->db && mysql_select_db($db,$this->db)) {
$this->connected = true;
}
if (isset($a->config["system"]["db_charset"]))
mysql_set_charset($a->config["system"]["db_charset"], $this->db);
}
if(! $this->connected) {
$this->db = null;
@@ -95,6 +99,14 @@ class dba {
$this->error = '';
// Check the connection (This can reconnect the connection - if configured)
if ($this->mysqli)
$connected = $this->db->ping();
else
$connected = mysql_ping($this->db);
$connstr = ($connected ? "Connected": "Disonnected");
$stamp1 = microtime(true);
if($this->mysqli)
@@ -122,14 +134,17 @@ class dba {
}
if($this->mysqli) {
if($this->db->errno)
if($this->db->errno) {
$this->error = $this->db->error;
$this->errorno = $this->db->errno;
}
} elseif(mysql_errno($this->db)) {
$this->error = mysql_error($this->db);
$this->errorno = mysql_errno($this->db);
}
elseif(mysql_errno($this->db))
$this->error = mysql_error($this->db);
if(strlen($this->error)) {
logger('dba: ' . $this->error);
logger('DB Error ('.$connstr.') '.$this->errorno.': '.$this->error);
}
if($this->debug) {

View File

@@ -260,6 +260,13 @@ function db_field_command($parameters, $create = true) {
function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
global $a, $db;
if (isset($a->config["system"]["db_charset"]))
$charset = $a->config["system"]["db_charset"];
elseif ($verbose)
$charset = "utf8mb4";
else
$charset = "utf8";
$r = true;
$sql = "";
@@ -282,7 +289,7 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
$sql = implode(",\n\t", $sql_rows);
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
$sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=".$charset;
if ($verbose)
echo $sql.";\n";