简体   繁体   中英

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'); function.

But after this, some characters like Ş, Ö started looking like Ö , Åž

How can i replace this kinda letters in mysql with UTF-8 format ?

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

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.

<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

  // 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:

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
  • The PHP headers and HTML headers should also be set to UTF-8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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