简体   繁体   English

php mysql特殊字符

[英]php mysql special character

My table looks like this- 我的桌子看起来像这样 -

province_id int(11)                  
province_title char(200) utf8_general_ci 
parent int(8) int(2)

I have inserted a lot of data direct from phpMyadmin (not from php page). 我直接从phpMyadmin插入了大量数据(不是从php页面)。 but when i retrieve those data and show them in php page, then there are problems with special characters. 但是当我检索这些数据并在php页面中显示它们时,那​​么特殊字符就会出现问题。

like- 喜欢-

Québec shows -  Qu�bec.

my html header looks like - 我的html标题看起来像 -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

i think the problem might have happened while inserting the data. 我认为插入数据时可能会发生问题。 how can i convert those data in phpmyadmin? 我怎样才能在phpmyadmin中转换这些数据? any help? 任何帮助?

seems like you're not using utf-8 everywhere so your data got messed up at some point. 好像你没有在任何地方使用utf-8 所以你的数据在某些时候搞砸了。 depending on what exactly you're doing, you'll have to change/add one or more of the following points (most likely it's the SET CHARSET / mysql_set_charset wich you forgot): 根据你究竟在做什么,你必须改变/添加以下一个或多个点(很可能是你忘记了SET CHARSET / mysql_set_charset ):

  • tell MySQL to use utf-8. 告诉MySQL使用utf-8。 to do this, add this to your my.cnf: 为此,请将此添加到my.cnf:

     collation_server = utf8_unicode_ci character_set_server = utf8 
  • before interacting with mysql, send this two querys: 在与mysql交互之前,发送以下两个查询:

     SET NAMES 'utf8'; CHARSET 'utf8'; 

    or, alternatively, let php do this after opening the connection: 或者,让php在打开连接后执行此操作:

     mysql_set_charset('utf8', $conn); // when using the mysql_-functions mysqli::set_charset('utf8') // when using mysqli 
  • set UTF-8 as the default charset for your database 将UTF-8设置为数据库的默认字符集

     CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8'; 
  • do the same for tables: 为表做同样的事情:

     CREATE TABLE `my_table` ( -- ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
  • assuming the client is a browser, serve your content as utf-8 and the the correct header: 假设客户端是浏览器,请将您的内容作为utf-8和正确的标头提供:

     header('Content-type: text/html; charset=utf-8'); 

    to be really sure the browser understands, add a meta-tag: 要确定浏览器能够理解,请添加元标记:

     <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
  • and, last but not least, tell the browser to submit forms using utf-8 最后但并非最不重要的是,告诉浏览器使用utf-8提交表单

     <form accept-charset="utf-8" ...> 

The problem is probably the character set of your connection to MySQL. 问题可能是您与MySQL连接的字符集。 See mysql_set_charset or mysqli::set_charset 请参阅mysql_set_charsetmysqli :: set_charset

MySQL server is not configured to expect UTF-8 encoding by default from client. MySQL服务器未配置为默认情况下从客户端期望UTF-8编码。

So you have to open connection using 所以你必须使用打开连接

mysql_query("SET NAMES utf8");   

If you are using PDO, 如果您使用的是PDO,

$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 

$dbh = new PDO($dsn, $username, $password, $options);

If it looks ok when you see the data in phpmyadmin the problem may be with the text enconding of the file itself. 如果在phpmyadmin中看到数据时看起来没问题,那么问题可能在于文件本身的文本内容。

Use Eclipse (Edit » Set Encoding) or Notepad++ (Encoding) to confirm that you have the right encoding in the file. 使用Eclipse(编辑»设置编码)或Notepad ++(编码)确认您在文件中具有正确的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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