Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用mysql#u set#u字符集(';utf8';)函数后用UTF-8替换字符_Php_Utf 8 - Fatal编程技术网

Php 使用mysql#u set#u字符集(';utf8';)函数后用UTF-8替换字符

Php 使用mysql#u set#u字符集(';utf8';)函数后用UTF-8替换字符,php,utf-8,Php,Utf 8,我将所有mysql表转换为utf-8_unicode,并开始使用mysql_set_字符集('utf8')函数 但在此之后,像Ş,Ö这样的角色开始看起来像Ã-,ž 如何用UTF-8格式替换mysql中的此类字母 很快,我能找到所有这些角色的列表来替换吗 编辑: 他在这篇文章中解释了这个问题,但我不能准确地理解它 你不需要那样做。只需在数据库连接后使用此代码 mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET utf8")

我将所有mysql表转换为utf-8_unicode,并开始使用
mysql_set_字符集('utf8')函数

但在此之后,像Ş,Ö这样的角色开始看起来像Ã-,ž

如何用UTF-8格式替换mysql中的此类字母

很快,我能找到所有这些角色的列表来替换吗

编辑: 他在这篇文章中解释了这个问题,但我不能准确地理解它


你不需要那样做。只需在数据库连接后使用此代码

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
并在所有页面中使用utf-8字符集

<meta http-equiv="Content-Type" content="text/html; charset=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);

?>

转换后,需要考虑其他因素,以确保一切正常工作:

  • 使用的PHP文件应使用UTF-8编码
  • PHP标头和HTML标头也应设置为UTF-8

查看此页面,有人建议我使用mysql_set_charset,而不是set name,因为我在使用它之前就已经使用过它了,而且它工作得很好。正如他所说,我同时使用set name和set charset。实际上问题不在于打印文本,而在于mysql中存储的文本。我得换了,我不明白。现在我正在使用这些代码,当我在数据库中插入一些东西时,它会存储为“öğı”,并像存储一样打印出来。你不需要更换任何东西。-->>艾哈迈特·贝伊·德迪基尔德·亚帕桑·希比尔·索伦·亚马亚卡克斯·纳兹。维里塔班·比雷伊·埃克莱迪姆·纳萨伊尔·凯迪约尔,亚兹德·卡迪约尔·奥杜古·吉比·亚兹约尔·索伦·约亚尼,这个答案是危险的。警告如果不调用mysql\u set\u charset(),mysql\u real\u escape\u string()将无法正确转义所有字符,这可能会导致SQL注入攻击。mysql_set_charset()文档页面警告您不要直接调用“set NAMES'utf8'”和类似的名称,这是有原因的。
http://www.yoursite.com/db_charset.php