Heim Datenbank MySQL-Tutorial 修复 MySQL页面正常 数据库文件sql乱码

修复 MySQL页面正常 数据库文件sql乱码

Jun 07, 2016 pm 05:11 PM

MySQL 页面正常 数据库文件sql乱码如果你的库原来是gbk编码,则把以下代码存为php文件,记得把下面的数据库名跟数据库帐号密码填

MySQL 页面正常 数据库文件sql乱码


如果你的库原来是gbk编码,则把以下代码存为php文件,记得把下面的数据库名跟数据库帐号密码填上

define('DB_NAME', 'putyourdbnamehere');    // 数据库名
define('DB_USER', 'usernamehere');     // MySQL用户名
define('DB_PASSWORD', 'yourpasswordhere'); // 密码
define('DB_HOST', 'localhost');    // 很大可能你无需修改此项

function gbk_DB_Converter_DoIt() {
 $tables = array();
 $tables_with_fields = array();

 // Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
 // we have to make an a stand-alone/direct connection to the database.
 $link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
 mysql_select_db(DB_NAME, $link_id);

 // Gathering information about tables and all the text/string fields that can be affected
 // during the conversion to gbk.
 $resource = mysql_query("SHOW TABLES", $link_id);
 while ( $result = mysql_fetch_row($resource) )
  $tables[] = $result[0];

 if ( !empty($tables) ) {
  foreach ( (array) $tables as $table ) {
   $resource = mysql_query("EXPLAIN $table", $link_id);
   while ( $result = mysql_fetch_assoc($resource) ) {
    if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
     $tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " .  ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
   }
  }

  // Change all text/string fields of the tables to their corresponding binary text/string representations.
  foreach ( (array) $tables as $table )
   mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);

  // Change database and tables to gbk Character set.
  mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET gbk", $link_id);
  foreach ( (array) $tables as $table )
   mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET gbk", $link_id);

  // Return all binary text/string fields previously changed to their original representations.
  foreach ( (array) $tables_with_fields as $table => $fields ) {
   foreach ( (array) $fields as $field_type => $field_options ) {
    mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
   }
  }

  // Optimize tables and finally close the mysql link.
  foreach ( (array) $tables as $table )
   mysql_query("OPTIMIZE TABLE $table", $link_id);
  mysql_close($link_id);
 } else {
  die('There are no tables?');
 }

 return true;
}
gbk_DB_Converter_DoIt();
?>

随便放到一个可以访问到库的php站点下运行即可。

 

如果你的库是utf-8编码,则运行以下代码,,记得把下面的数据库名跟数据库帐号密码填上

define('DB_NAME', 'putyourdbnamehere');    // 数据库名
define('DB_USER', 'usernamehere');     // MySQL用户名
define('DB_PASSWORD', 'yourpasswordhere'); // 密码
define('DB_HOST', 'localhost');    // 很大可能你无需修改此项

function UTF8_DB_Converter_DoIt() {
$tables = array();
$tables_with_fields = array();

// Since we cannot use the WordPress Database Abstraction Class (wp-db.php),
// we have to make an a stand-alone/direct connection to the database.
$link_id = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Error establishing a database connection');
mysql_select_db(DB_NAME, $link_id);

// Gathering information about tables and all the text/string fields that can be affected
// during the conversion to UTF-8.
$resource = mysql_query("SHOW TABLES", $link_id);
while ( $result = mysql_fetch_row($resource) )
$tables[] = $result[0];

if ( !empty($tables) ) {
foreach ( (array) $tables as $table ) {
$resource = mysql_query("EXPLAIN $table", $link_id);
while ( $result = mysql_fetch_assoc($resource) ) {
if ( preg_match('/(char)|(text)|(enum)|(set)/', $result['Type']) )
$tables_with_fields[$table][$result['Field']] = $result['Type'] . " " . ( "YES" == $result['Null'] ? "" : "NOT " ) . "NULL " .  ( !is_null($result['Default']) ? "DEFAULT '". $result['Default'] ."'" : "" );
}
}

// Change all text/string fields of the tables to their corresponding binary text/string representations.
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET binary", $link_id);

// Change database and tables to UTF-8 Character set.
mysql_query("ALTER DATABASE " . DB_NAME . " CHARACTER SET utf8", $link_id);
foreach ( (array) $tables as $table )
mysql_query("ALTER TABLE $table CONVERT TO CHARACTER SET utf8", $link_id);

// Return all binary text/string fields previously changed to their original representations.
foreach ( (array) $tables_with_fields as $table => $fields ) {
foreach ( (array) $fields as $field_type => $field_options ) {
mysql_query("ALTER TABLE $table MODIFY $field_type $field_options", $link_id);
}
}

// Optimize tables and finally close the mysql link.
foreach ( (array) $tables as $table )
mysql_query("OPTIMIZE TABLE $table", $link_id);
mysql_close($link_id);
} else {
die('There are no tables?');
}

return true;
}
UTF8_DB_Converter_DoIt();
?>

运行完之后登录phpmyadmin,查询下就知道乱码已经恢复!

linux

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Wie verändern Sie eine Tabelle in MySQL mit der Änderungstabelleanweisung? Wie verändern Sie eine Tabelle in MySQL mit der Änderungstabelleanweisung? Mar 19, 2025 pm 03:51 PM

In dem Artikel werden mithilfe der Änderungstabelle von MySQL Tabellen, einschließlich Hinzufügen/Löschen von Spalten, Umbenennung von Tabellen/Spalten und Ändern der Spaltendatentypen, erläutert.

Wie konfiguriere ich die SSL/TLS -Verschlüsselung für MySQL -Verbindungen? Wie konfiguriere ich die SSL/TLS -Verschlüsselung für MySQL -Verbindungen? Mar 18, 2025 pm 12:01 PM

In Artikel werden die Konfiguration der SSL/TLS -Verschlüsselung für MySQL, einschließlich der Erzeugung und Überprüfung von Zertifikaten, erläutert. Das Hauptproblem ist die Verwendung der Sicherheitsauswirkungen von selbstsignierten Zertifikaten. [Charakterzahl: 159]

Wie behandeln Sie große Datensätze in MySQL? Wie behandeln Sie große Datensätze in MySQL? Mar 21, 2025 pm 12:15 PM

In Artikel werden Strategien zum Umgang mit großen Datensätzen in MySQL erörtert, einschließlich Partitionierung, Sharding, Indexierung und Abfrageoptimierung.

Was sind einige beliebte MySQL -GUI -Tools (z. B. MySQL Workbench, PhpMyAdmin)? Was sind einige beliebte MySQL -GUI -Tools (z. B. MySQL Workbench, PhpMyAdmin)? Mar 21, 2025 pm 06:28 PM

In Artikel werden beliebte MySQL -GUI -Tools wie MySQL Workbench und PhpMyAdmin beschrieben, die ihre Funktionen und ihre Eignung für Anfänger und fortgeschrittene Benutzer vergleichen. [159 Charaktere]

Wie lassen Sie eine Tabelle in MySQL mit der Drop -Tabelle -Anweisung fallen? Wie lassen Sie eine Tabelle in MySQL mit der Drop -Tabelle -Anweisung fallen? Mar 19, 2025 pm 03:52 PM

In dem Artikel werden in MySQL die Ablagerung von Tabellen mithilfe der Drop -Tabellenerklärung erörtert, wobei Vorsichtsmaßnahmen und Risiken betont werden. Es wird hervorgehoben, dass die Aktion ohne Backups, die Detaillierung von Wiederherstellungsmethoden und potenzielle Produktionsumfeldgefahren irreversibel ist.

Wie repräsentieren Sie Beziehungen mit fremden Schlüsseln? Wie repräsentieren Sie Beziehungen mit fremden Schlüsseln? Mar 19, 2025 pm 03:48 PM

In Artikeln werden ausländische Schlüssel zur Darstellung von Beziehungen in Datenbanken erörtert, die sich auf Best Practices, Datenintegrität und gemeinsame Fallstricke konzentrieren.

Wie erstellen Sie Indizes für JSON -Spalten? Wie erstellen Sie Indizes für JSON -Spalten? Mar 21, 2025 pm 12:13 PM

In dem Artikel werden in verschiedenen Datenbanken wie PostgreSQL, MySQL und MongoDB Indizes für JSON -Spalten in verschiedenen Datenbanken erstellt, um die Abfrageleistung zu verbessern. Es erläutert die Syntax und die Vorteile der Indizierung spezifischer JSON -Pfade und listet unterstützte Datenbanksysteme auf.

Wie sichere ich mich MySQL gegen gemeinsame Schwachstellen (SQL-Injektion, Brute-Force-Angriffe)? Wie sichere ich mich MySQL gegen gemeinsame Schwachstellen (SQL-Injektion, Brute-Force-Angriffe)? Mar 18, 2025 pm 12:00 PM

Artikel erläutert die Sicherung von MySQL gegen SQL-Injektions- und Brute-Force-Angriffe unter Verwendung vorbereiteter Aussagen, Eingabevalidierung und starken Kennwortrichtlinien (159 Zeichen).

See all articles