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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9864711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:48:43  来源:igfitidea点击:

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

phputf-8

提问by Utku Dalmaz

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

我将所有 mysql 表转换为 utf-8_unicode 并开始使用mysql_set_charset('utf8');函数。

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

编辑:他实际上在这篇文章中解释了这个问题,但我无法正确理解它,哈哈

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

回答by guybennet

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

回答by Zuul

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,将它上传到您网站的根目录并从浏览器执行它:

e.g.,

例如,

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
  • 正在使用的 PHP 文件应使用 UTF-8 编码
  • PHP 标头和 HTML 标头也应设置为 UTF-8