اخر المواضيع

آخر الأخبار
إختر لونك المفضل

عرض المحتوي

بحث

الجمعة، 5 يناير 2024

تغير اللغة بعد اضافه القاعدة لاي سكربت convert utf-8 to utf8mb4 localhost

السفراني | يناير 05, 2024 |
السلام عليكم

في مجلد convert utf-8 to utf8mb4
تنزيل
تجد قالب باسم convert.php
بس تضعه في اي مجلد يكون التشعيل بالمثال
http://localhost/vb/convert.php
http://www./vb/convert.php
انتبه لوجوده بالمجلد المطلوب كمثال
vb
وقبل التطبيق لزم يكون القاعده مفعله علشان يشتغل معك
المحتوي : لـ convert.php
$host = 'localhost';
$username= 'root'; اسم القاعده
$password = '12345'; الرفم السري لقاعده
$database = 'forum'; اسم القاعده لمجلد
فقط وهذا نموذج لتطبيق

حفظ الكود سمه مثلا باسم :
convert.php
الملف

<?php
/**
*
* Step 1 Source: https://stackoverflow.com/questions/6115612/how-to-convert-an-entire-mysql-database-characterset-and-collation-to-utf-8
*
* Step 2 Source: https://gist.github.com/hollodotme/fe24b961680e08473072
*
*/

/**
* Requires php >= 5.5
*
* Use this script to convert utf-8 data in utf-8 mysql tables stored via latin1 connection
* This is a PHP port from: https://gist.github.com/njvack/6113127
*
* @link : http://www.ridesidecar.com/2013/07/30/of-databases-and-character-encodings/
*
* BACKUP YOUR DATABASE BEFORE YOU RUN THIS SCRIPT!
*
* Once the script ran over your databases, change your database connection charset to utf8mb4:
*
* $dsn = 'mysql:host=localhost;port=3306;charset=utf8mb4';
*
* DON'T RUN THIS SCRIPT MORE THAN ONCE!
*
* @author hollodotme
*/

/*
include "engine/config.php";
$host = $config['db']['server'];
$username= $config['db']['username'];
$password = $config['db']['password'];
$database = $config['db']['name'];
*/

$host = 'localhost';
$username= 'root';
$password = '12345';
$database = 'forum';


if (version_compare(phpversion(), '5.4', '<=')) {
die('PHP version must be >= 5.4');
}
if (isset($_GET['step'])) {
if ($_GET['step'] == 1) {
$conn = mysqli_connect($host, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$alter_database_charset_sql = "ALTER DATABASE " . $database . " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
mysqli_query($conn, $alter_database_charset_sql);
$show_tables_result = mysqli_query($conn, "SHOW TABLES");
$tables = mysqli_fetch_all($show_tables_result);
echo '<pre style="width:500px;height:300px;overflow-y:scroll;border:1px solid #cdcdcd">';
foreach ($tables as $index => $table) {
$alter_table_sql = "ALTER TABLE " . $table[0] . " CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
$alter_table_result = mysqli_query($conn, $alter_table_sql);
var_dump($alter_table_result);
}
echo '</pre>';
// echo '<h1>OK :) Go to <a href="?step=2">Step 2</a></h1>';
}
elseif ($_GET['step'] == 2) {
if (isset($_GET['step']) && $_GET['step'] == 2) {
if (isset($_COOKIE['Converted']) && $_COOKIE['Converted'] == $database) {
die('<h1>Database converted! <p style="color:red">DO NOT RECONVERT IT AGAIN!, <br>that will distroy your database data.</p></h1>');
}
setcookie("Converted", $database, time() + 3600 * 24 * 30);

header('Content-Type: text/html; charset=utf-8');
$dsn = 'mysql:host=localhost;port=3306;charset=latin1';
$user = $username;
$password = $password;
$options = [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY, \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET latin1",];
$dbManager = new \PDO($dsn, $user, $password, $options);
$databasesToConvert = [$database];
$typesToConvert = ['char', 'varchar', 'tinytext', 'mediumtext', 'text', 'longtext'];
echo '<pre style="width:500px;height:300px;overflow-y:auto">';
foreach ($databasesToConvert as $database) {
echo $database, ":\n";
echo str_repeat('=', strlen($database) + 1), "\n";
$dbManager->exec("USE `{$database}`");
$tablesStatement = $dbManager->query("SHOW TABLES");
while (($table = $tablesStatement->fetchColumn())) {
echo "Table: {$table}:\n";
echo str_repeat('-', strlen($table) + 8), "\n";
$columnsToConvert = [];
$columsStatement = $dbManager->query("DESCRIBE `{$table}`");
while (($tableInfo = $columsStatement->fetch(\PDO::FETCH_ASSOC))) {
$column = $tableInfo['Field'];
echo ' * ' . $column . ': ' . $tableInfo['Type'];
$type = preg_replace("#\(\d+\)#", '', $tableInfo['Type']);
if (in_array($type, $typesToConvert)) {
echo " => must be converted\n";
$columnsToConvert[] = $column;
}
else {
echo " => not relevant\n";
}
}
if (!empty($columnsToConvert)) {
$converts = array_map(
function ($column) {
return "`{$column}` = CONVERT(CAST(CONVERT(`{$column}` USING latin1) AS binary) USING utf8mb4)";
}
, $columnsToConvert );
$query = "UPDATE `{$table}` SET " . join(', ', $converts);
echo "\n", $query, "\n";
$dbManager->exec($query);
}
echo "\n--\n";
}
echo "\n";
}
echo '</pre>';
echo '<h1>Done :)</h1>';
}
}
}
else {
echo '<h1>Convert utf8 to utf8mb4<br><a href="?step=1">Start</a></h1>';
}
?>

وسلامتكم

author

ليست هناك تعليقات:

اضف تعليقك هنا