我正在使用一个过程来转储数据库内容并编写一个文件 sql ...
一切正常,但在我的文件中,所有具有特定字符(如 è、àù éç 等)的数据都在文件中丢失了......我现在有点困惑。...我必须在哪里设置正确的字符集?
我的数据库有一个默认字符集 UTF8 和排序规则 UTF8_general_ci
在我的服务器端脚本中从数据库中正确处理数据(CRUD 正常工作并且数据正确显示)。当我在变量中获取数据并将完整的 SQL 过程写到文件中时,重音字母会损坏......
这是我的转储程序,它返回我的数据库的完整转储
==================================================== =======================
函数转储数据库(){
// 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;
}
}