简体   繁体   English

使用mysql_set_charset('utf8')函数后,用UTF-8替换字符

[英]replacing characters with UTF-8 after using mysql_set_charset('utf8') function

I converted all mysql tables to utf-8_unicode and started using mysql_set_charset('utf8'); 我将所有mysql表转换为utf-8_unicode并开始使用mysql_set_charset('utf8'); function. 功能。

But after this, some characters like Ş, Ö started looking like Ö , Åž 但在此之后,像Ş,Ö这样的角色开始看起来像Ã,Åž

How can i replace this kinda letters in mysql with UTF-8 format ? 如何用UTF-8格式替换mysql中的这种字母?

shortly, can i find a list of all these kinda characters to replace ? 很快,我能找到所有这些有意义的角色的名单来取代吗?

EDIT: He is explaining about this issue in this article actually but i cannot understand it properly acutally lol 编辑:他实际上是在这篇文章中解释这个问题,但我无法理解它的实际操作lol

http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html

you dont need to do that. 你不需要这样做。 just use this code after database connect. 只需在数据库连接后使用此代码。

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");

and use utf-8 charset in all of your pages. 并在所有页面中使用utf-8字符集。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

This PHP script allows you to convert your existing database and tables to UTF-8: 此PHP脚本允许您将现有数据库和表转换为UTF-8:

<?php

  // Database connection details 
  $db_srv = 'localhost';
  $db_usr = 'user'; 
  $db_pwd = 'password'; 
  $db_name = 'database name';

  // New charset information, update accordingly if not UTF8
  $char_set = 'utf8';
  $char_collation = 'utf8_general_ci';

  header('Content-type: text/plain'); 

  // extablish the connection
  $connection = mysql_connect($db_srv,$db_usr,$db_pwd) or die(mysql_error()); 

  // select the databse
  $db = mysql_select_db($db_name) or die(mysql_error()); 

  // get existent tables
  $sql = 'SHOW TABLES';
  $res = mysql_query($sql) or die(mysql_error()); 

  // for each table found
  while ($row = mysql_fetch_row($res))
  {   
    // change the table charset
    $table = mysql_real_escape_string($row[0]); 

    $sql = " ALTER TABLE ".$table." 
             DEFAULT CHARACTER SET ".$char_set." 
             COLLATE ".$char_collation; 

    mysql_query($sql) or die(mysql_error());

    echo 'The '.$table.' table was updated successfully to: '.$char_set."\n";
  }

  // Update the Collation of the database itself
  $sql = "ALTER DATABASE CHARACTER SET ".$char_set.";";
  mysql_query($sql) or die(mysql_error());

  echo 'The '.$db_name.' database collation';
  echo ' has been updated successfully to: '.$char_set."\n";

  // close the connection to the database
  mysql_close($connection);

?>

Save it on a file, lets say db_charset.php , upload it to the root of your website and execute it from the browser: 将其保存在文件中,比如说db_charset.php ,将其上传到您网站的根目录并从浏览器执行:

eg, 例如,

http://www.yoursite.com/db_charset.php

Other considerations are necessary to have everything working properly after the conversion: 在转换后使一切正常工作需要其他注意事项:

  • The PHP files being used should be encoded with UTF-8 正在使用的PHP文件应使用UTF-8编码
  • The PHP headers and HTML headers should also be set to UTF-8 PHP标头和HTML标头也应设置为UTF-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM