Skip to content

Instantly share code, notes, and snippets.

@webuti
Created August 5, 2018 11:20
Show Gist options
  • Save webuti/a0516a141858482acaa5a1212c3bf2f4 to your computer and use it in GitHub Desktop.
Save webuti/a0516a141858482acaa5a1212c3bf2f4 to your computer and use it in GitHub Desktop.
latin5 to utf8
<?php
ob_start();
header('Content-Type: text/html; charset=utf-8');
$servername = '127.0.0.1'; // MySQL hostname
$dbuser = 'uu'; // MySQL user
$dbuser_pass = 'rr'; // MySQL password
$dbname = 'dd'; // MySQL database name containing phpSecurePages table
$charset = "latin5";
$servername2 = '127.1.1.1'; // MySQL 2 hostname
$dbuser2 = 'uu'; // MySQL 2 user
$dbuser_pass2 = 'rr'; // MySQL 2 password
$dbname2 = 'dd'; // MySQL 2 database name containing phpSecurePages table
$charset2 = "utf8";
$link = mysqli_connect($servername, $dbuser, $dbuser_pass, $dbname);
mysqli_set_charset($link, $charset);
$link2 = mysqli_connect($servername2, $dbuser2, $dbuser_pass2, $dbname2);
mysqli_set_charset($link2, $charset2);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$page = intval($_GET['page']);
$tableKey = intval($_GET['tablo']);
$tableField = [];
$sql = mysqli_query($link, "SHOW TABLES ");
$table['Tables_in_'.$dbname] = 'xFirms';
$tables = [];
while ($table = mysqli_fetch_assoc($sql)) {
$ysql = mysqli_query($link, "SHOW COLUMNS FROM `" . $table['Tables_in_'.$dbname] . "`");
$tables [] = $table['Tables_in_'.$dbname];
while ($result = mysqli_fetch_assoc($ysql)) {
if ($result['Field']) {
// $tableField[$table['Tables_in_'.$dbname]][] = $result['Field'];
}
}
}
$table['Tables_in_'.$dbname] = $tables[$tableKey];
$ysql = mysqli_query($link, "SHOW COLUMNS FROM `" . $tables[$tableKey] . "`");
while ($result = mysqli_fetch_assoc($ysql)) {
if ($result['Field']) {
$tableField[$table['Tables_in_'.$dbname]][] = $result['Field'];
}
}
foreach ($tableField as $key => $items) {
$xysql = mysqli_query($link, "SELECT * FROM " . $key . " limit " . $page . ",20000");
$countSql = mysqli_query($link, "SELECT count(*) as say FROM " . $key);
$rtt = mysqli_fetch_assoc($countSql);
if ($xysql) {
$insert_sql = '';
while ($data = mysqli_fetch_assoc($xysql)) {
if ($data) {
$insert_sql .= "REPLACE INTO `" . $key . "` SET ";
foreach ($items as $item) {
$insert_sql .= " `" . $item . "` = '" . addslashes(iconv("ISO-8859-9", "UTF-8", $data[$item])) . "',";
}
$insert_sql = substr($insert_sql, 0, -1);
$insert_sql .= "; \n ";
}
if (!mysqli_query($link2, $insert_sql)) {
echo $insert_sql;
$insert_sql = '';
echo("Error description: " . mysqli_error($link2));
exit;
}
$insert_sql = '';
}
if ($rtt['say'] > 19999) {
echo '<meta http-equiv="refresh" content="1;URL=';
echo "migrationscript.php?tablo=" . ($tableKey) . "&page=" . ($_GET['page'] + 10000) . '">';
} else {
echo 'NEXT GO GO GO >>';
echo '<meta http-equiv="refresh" content="1;URL=';
echo "migrationscript.php?tablo=" . ($tableKey + 1) . "&page=0";
echo '">';
}
} else {
echo 'NEXT GO GO GO >>';
echo '<meta http-equiv="refresh" content="1;URL=';
echo "migrationscript.php?tablo=" . ($tableKey + 1) . "&page=0";
echo '">';
}
if ($_GET['page'] > $rtt['say']) {
echo 'NEXT GO GO GO >>';
echo '<meta http-equiv="refresh" content="1;URL=';
echo "migrationscript.php?tablo=" . ($tableKey + 1) . "&page=0";
echo '">';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment