转换错误的编码 - MySQL
Convert wrong encoding - MySQL
我的网站有 MySQL 数据库,里面已经有很多数据,我在数据库编码上犯了一个错误,现在所有数据都无法通过“PHPMyAdmin”读取
现在我解决了编码问题,所有新数据都是可读的,但旧数据不行,问题出在与数据库的连接上,因为我没有添加“utf8”聊天集...
如果我从连接中删除 mysqli_set_charset($con,"utf8");
,数据 OLD 是可读的
但是 NEW 数据不可读。
有什么方法可以将错误的编码数据转换为正确的UTF8?
带有好线和坏线的图像示例:
好吧,我认为最好的解决方案是手动更新,
进行数据库备份、配置并将此文件放在您的网络服务器中的任何位置并使用它。
编辑 我忘了说添加一个名为 reconfigured 的新列到此 table
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "database";
$table = "corrupted table";
//utf8
$con = mysqli_connect($server, $username, $password, $database);
mysqli_set_charset($con,"utf8");
//auto
$con2 = mysqli_connect($server, $username, $password, $database);
if (isset($_GET['id']) and isset($_GET['which'])) {
switch($_GET['which']) {
case 1: $correct_string = mysqli_query($con, "SELECT name FROM `".$table." WHERE `id`='".$_GET['id']."'"); $correct_string = mysqli_fetch_row($con, $correct_string); break;
case 2: $correct_string = mysqli_query($con2, "SELECT name FROM `".$table." WHERE `id`='".$_GET['id']."'"); $correct_string = mysqli_fetch_row($con2, $correct_string); break;
}
mysqli_query($con, "UPDATE `".$table."` SET `name` = '".$correct_string['0']."' WHERE `".$table."`.`id` = '".$_GET['id']."'");
mysqli_query($con, "UPDATE `".$table."` SET `reconfigured` = '1' WHERE `".$table."`.`id` = '".$_GET['id']."'");
}
$string1 = mysqli_query($con, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string2 = mysqli_query($con2, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string1 = mysqli_fetch_row($con, $string1);
$string2 = mysqli_fetch_row($con2, $string2);
$string1 = $string1['0'];
$string2 = $string2['0'];
?>
<form action="">
<input type="text" id="id" name="id" value="<?= $id ?>"></input><br>
<input type="radio" id="which" name="which" value="1"><?= $string1 ?><br>
<input type="radio" id="which" name="which" value="2"><?= $string2 ?><br><br>
<input type="submit" value="continue">
</form>
我没有测试过这个脚本,谨慎使用并备份数据库
edit,这种方式是自动的,如果它有一个坏词(比如 –),它会用另一种方法更新它,我仔细检查了代码,但你检查了也再次备份数据库,我有点不相信自己
<?php
$broken = array('×', '¢', 'œ', 'ª');
$server = "localhost";
$username = "root";
$password = "";
$database = "database";
$table = "corrupted table";
//utf8
$con = mysqli_connect($server, $username, $password, $database);
mysqli_set_charset($con,"utf8");
//auto
$con2 = mysqli_connect($server, $username, $password, $database);
$fixed = 0;
while ($fixed == 1) {
$string1 = mysqli_query($con, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string2 = mysqli_query($con2, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string1 = mysqli_fetch_row($con, $string1);
$string2 = mysqli_fetch_row($con2, $string2);
if(implode(null,$string1) == null){ die('done'); }
if(implode(null,$string2) == null){ die('done'); }
$string1 = $string1['0'];
$string2 = $string2['0'];
while ($found == 1) {
foreach ($broken as $badword) {
if(strpos($string1, $badword)) { $found = 1; $broken = 1; }
}
$found = 1;
}
if ($broken == 1) {
mysqli_query($con, "UPDATE `".$table."` SET `name` = '".$string2."' WHERE `".$table."`.`id` = '".$id."'");
}
mysqli_query($con, "UPDATE `".$table."` SET `reconfigured` = '1' WHERE `".$table."`.`id` = '".$id."'");
unset($broken);
unset($found);
}
?>
我的网站有 MySQL 数据库,里面已经有很多数据,我在数据库编码上犯了一个错误,现在所有数据都无法通过“PHPMyAdmin”读取
现在我解决了编码问题,所有新数据都是可读的,但旧数据不行,问题出在与数据库的连接上,因为我没有添加“utf8”聊天集...
如果我从连接中删除 mysqli_set_charset($con,"utf8");
,数据 OLD 是可读的
但是 NEW 数据不可读。
有什么方法可以将错误的编码数据转换为正确的UTF8?
带有好线和坏线的图像示例:
好吧,我认为最好的解决方案是手动更新,
进行数据库备份、配置并将此文件放在您的网络服务器中的任何位置并使用它。
编辑 我忘了说添加一个名为 reconfigured 的新列到此 table
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "database";
$table = "corrupted table";
//utf8
$con = mysqli_connect($server, $username, $password, $database);
mysqli_set_charset($con,"utf8");
//auto
$con2 = mysqli_connect($server, $username, $password, $database);
if (isset($_GET['id']) and isset($_GET['which'])) {
switch($_GET['which']) {
case 1: $correct_string = mysqli_query($con, "SELECT name FROM `".$table." WHERE `id`='".$_GET['id']."'"); $correct_string = mysqli_fetch_row($con, $correct_string); break;
case 2: $correct_string = mysqli_query($con2, "SELECT name FROM `".$table." WHERE `id`='".$_GET['id']."'"); $correct_string = mysqli_fetch_row($con2, $correct_string); break;
}
mysqli_query($con, "UPDATE `".$table."` SET `name` = '".$correct_string['0']."' WHERE `".$table."`.`id` = '".$_GET['id']."'");
mysqli_query($con, "UPDATE `".$table."` SET `reconfigured` = '1' WHERE `".$table."`.`id` = '".$_GET['id']."'");
}
$string1 = mysqli_query($con, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string2 = mysqli_query($con2, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string1 = mysqli_fetch_row($con, $string1);
$string2 = mysqli_fetch_row($con2, $string2);
$string1 = $string1['0'];
$string2 = $string2['0'];
?>
<form action="">
<input type="text" id="id" name="id" value="<?= $id ?>"></input><br>
<input type="radio" id="which" name="which" value="1"><?= $string1 ?><br>
<input type="radio" id="which" name="which" value="2"><?= $string2 ?><br><br>
<input type="submit" value="continue">
</form>
我没有测试过这个脚本,谨慎使用并备份数据库
edit,这种方式是自动的,如果它有一个坏词(比如 –),它会用另一种方法更新它,我仔细检查了代码,但你检查了也再次备份数据库,我有点不相信自己
<?php
$broken = array('×', '¢', 'œ', 'ª');
$server = "localhost";
$username = "root";
$password = "";
$database = "database";
$table = "corrupted table";
//utf8
$con = mysqli_connect($server, $username, $password, $database);
mysqli_set_charset($con,"utf8");
//auto
$con2 = mysqli_connect($server, $username, $password, $database);
$fixed = 0;
while ($fixed == 1) {
$string1 = mysqli_query($con, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string2 = mysqli_query($con2, "SELECT name FROM `".$table."` WHERE reconfigured = '0' LIMIT 1");
$string1 = mysqli_fetch_row($con, $string1);
$string2 = mysqli_fetch_row($con2, $string2);
if(implode(null,$string1) == null){ die('done'); }
if(implode(null,$string2) == null){ die('done'); }
$string1 = $string1['0'];
$string2 = $string2['0'];
while ($found == 1) {
foreach ($broken as $badword) {
if(strpos($string1, $badword)) { $found = 1; $broken = 1; }
}
$found = 1;
}
if ($broken == 1) {
mysqli_query($con, "UPDATE `".$table."` SET `name` = '".$string2."' WHERE `".$table."`.`id` = '".$id."'");
}
mysqli_query($con, "UPDATE `".$table."` SET `reconfigured` = '1' WHERE `".$table."`.`id` = '".$id."'");
unset($broken);
unset($found);
}
?>