2012-03-25 1 views
3

모든 mysql 테이블을 utf-8_unicode로 변환하고 mysql_set_charset('utf8'); 함수를 사용하기 시작했습니다. mysql_set_charset ('utf8') 함수를 사용한 후 문자를 UTF-8로 바꾸기

그러나이 후

, S와 같은 일부 문자는, Ö는

어떻게 UTF-8 형식으로 MySQL의이 좀 문자를 대체 할 수있는, Ã - 같은 AZ

을 찾기 시작?

곧, 대체 할 이러한 모든 문자 목록을 곧 찾을 수 있습니까?

편집 : 그는 실제로이 문서에서이 문제에 대해 설명되어 있지만 내가 롤 제대로 acutally을 이해할 수 없다 당신이 그나마

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

답변

10

그렇게해야합니다. 데이터베이스 연결 후이 코드를 사용하십시오.

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

모든 페이지에서 utf-8 charset을 사용하십시오. , 파일에 저장을

<?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); 

?> 

, db_charset.php 말을 당신의 웹 사이트의 루트에 업로드하고 있습니다 :

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

이 페이지를 확인하면, 누군가 내가 설정 이름 대신 mysql_set_charset을 사용하도록 제안했다. 이전에이 설정을 사용하고 있었으므로 http://stackoverflow.com/questions/9703536/bad-characters-when-printing-text- from-utf8-unicode-ci-mysql-table –

+1

ı 이것을 사용하면 완벽하게 작동합니다. 그가 사용하는 것처럼 두 가지 모두 사용하고 이름을 설정하고 charset을 설정합니다. – guybennet

+0

실제로 문제는 인쇄 된 텍스트가 아니라 문제는 mysql에 저장된 텍스트에 관한 것입니다. 나는 그들을 대체해야한다. –

4

이 PHP 스크립트는 UTF-8로 기존 데이터베이스와 테이블을 변환 할 수 있습니다 브라우저에서 실행 : 예

,

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

다른 고려 사항은 변환 후 제대로 작동 모든 것을 가질 필요하다 :

  • UTF-8
  • PHP의 헤더와 HTML 헤더도 설정해야합니다 인코딩해야 사용중인 PHP 파일 UTF-8
관련 문제