Replacing UTF-8 characters after using mysql_set_charset ('utf8') function

I converted all mysql tables to utf-8_unicode and started using the function mysql_set_charset('utf8');

.

But after that some characters like Ş, ... started to look like Ã-, Åž

How can I replace these curious letters in mysql with UTF-8 format?

anytime soon, can I find a list of all these kinds of replacement characters?

EDIT: He explains this issue in this article, but I can't get it right. Lol

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

+3


source to share


2 answers


you don't need to do this. just use this code after connecting the database.

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

      



and use utf-8 encoding on all your pages.

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

      

+10


source


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 to a file, say db_charset.php

upload it to the root of your site and execute it from your browser:

eg.,



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

      


Other considerations needed to function properly after conversion are needed:

  • PHP files used must be UTF-8 encoded
  • PHP headers and HTML headers must also be set to UTF-8
+4


source







All Articles