我正在使用一个过程来转储数据库内容并编写一个文件sql ...
一切正常但在我的文件中所有具有特定字符的数据(如è,àù等等......)都会丢失在文件中...我现在有点困惑。 ...我必须设置正确的字符集?
我的数据库有一个默认字符集UTF8和Collation UTF8_general_ci
我的服务器端脚本中的数据库正确处理数据(CRUD工作正常,数据显示正确)。当我在变量中获取数据并将完整的SQL过程写入文件时,重音字母会被破坏...
这是我的转储程序,它返回我的db
的完整转储=============================================== =========================
function dumpDatabase(){
// Connect to database
$db = mysql_select_db($this->DATABASE_DB);
if (!$db) {
die ('Can\'t use foo : ' . mysql_error());
}
if (!empty($db)) {
// Get all table names from database
$c = 0;
$result = mysql_list_tables($this->DATABASE_DB);
for($x = 0; $x < mysql_num_rows($result); $x++) {
$table = mysql_tablename($result, $x);
if (!empty($table)) {
$arr_tables[$c] = mysql_tablename($result, $x);
$c++;
}
}
// Structure Header
$structure .= "-- \n";
$structure .= "-- Backup effettuato il `".date('Y-m-d H:i:s')."` \n";
$structure .= "-- Backup per database `{$this->DATABASE_DB}` \n";
$structure .= "-- \n\n";
$structure .= "-- \n\n";
$structure .= "-- \n\n";
$structure .= "SET NAMES utf8; \n";
$structure .= "SET FOREIGN_KEY_CHECKS = 0;";
$structure .= "-- \n\n";
$structure .= "-- \n\n";
// List tables
$dump = '';
for ($y = 0; $y < count($arr_tables); $y++){
// DB Table name
$table = $arr_tables[$y];
// Structure Header
$structure .= "-- \n";
$structure .= "-- Table structure for table `{$table}` \n";
$structure .= "-- \n\n";
// Dump Structure
$structure .= "DROP TABLE IF EXISTS `{$table}`; \n";
$structure .= "CREATE TABLE `{$table}` (\n";
$result = mysql_db_query($this->DATABASE_DB, "SHOW FIELDS FROM `{$table}`");
while($row = mysql_fetch_object($result)) {
$structure .= " `{$row->Field}` {$row->Type}";
$structure .= (!empty($row->Default)) ? " DEFAULT `{$row->Default}`" : false;
$structure .= ($row->Null != "YES") ? " NOT NULL" : false;
$structure .= (!empty($row->Extra)) ? strtoupper(" {$row->Extra}") : false;
$structure .= ",\n";
}
$structure = ereg_replace(",\n$", "", $structure);
// Save all Column Indexes in array
unset($index);
$result = mysql_db_query($this->DATABASE_DB, "SHOW KEYS FROM `{$table}`");
while($row = mysql_fetch_object($result)) {
if (($row->Key_name == 'PRIMARY') AND ($row->Index_type == 'BTREE')) {
$index['PRIMARY'][$row->Key_name] = $row->Column_name;
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '0') AND ($row->Index_type == 'BTREE')) {
$index['UNIQUE'][$row->Key_name] = $row->Column_name;
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'BTREE')) {
$index['INDEX'][$row->Key_name] = $row->Column_name;
}
if (($row->Key_name != 'PRIMARY') AND ($row->Non_unique == '1') AND ($row->Index_type == 'FULLTEXT')) {
$index['FULLTEXT'][$row->Key_name] = $row->Column_name;
}
}
// Return all Column Indexes of array
if (is_array($index)) {
foreach ($index as $xy => $columns) {
$structure .= ",\n";
$c = 0;
foreach ($columns as $column_key => $column_name) {
$c++;
$structure .= ($xy == "PRIMARY") ? " PRIMARY KEY (`{$column_name}`)" : false;
$structure .= ($xy == "UNIQUE") ? " UNIQUE KEY `{$column_key}` (`{$column_name}`)" : false;
$structure .= ($xy == "INDEX") ? " KEY `{$column_key}` (`{$column_name}`)" : false;
$structure .= ($xy == "FULLTEXT") ? " FULLTEXT `{$column_key}` (`{$column_name}`)" : false;
$structure .= ($c < (count($index[$xy]))) ? ",\n" : false;
}
}
}
$structure .= "\n);\n\n";
// Header
$structure .= "-- \n";
$structure .= "-- Dumping data for table `$table` \n";
$structure .= "-- \n\n";
// Dump data
unset($data);
//mysql_set_charset('latin1');
$result = mysql_query("SELECT * FROM `$table`");
$num_rows = mysql_num_rows($result);
$num_fields = mysql_num_fields($result);
for ($i = 0; $i < $num_rows; $i++) {
$row = mysql_fetch_object($result);
$data .= "INSERT INTO `$table` (";
// Field names
for ($x = 0; $x < $num_fields; $x++) {
$field_name = mysql_field_name($result, $x);
$data .= "`{$field_name}`";
$data .= ($x < ($num_fields - 1)) ? ", " : false;
}
$data .= ") VALUES (";
// Values
for ($x = 0; $x < $num_fields; $x++) {
$field_name = mysql_field_name($result, $x);
$data .= "'" . str_replace('\"', '"', mysql_escape_string($row->$field_name)) . "'";
$data .= ($x < ($num_fields - 1)) ? ", " : false;
}
$data.= ");\n";
}
$data.= "\n";
$dump .= $structure . $data;
$dump .= "-- --------------------------------------------------------\n\n";
}
return $dump;
}
}
答案 0 :(得分:1)
尝试在SET NAMES utf8;
之后添加此代码或使用mysqldump
实用程序。
SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8';