PHP的MySQL特殊字符 [英] php mysql special character

查看:106
本文介绍了PHP的MySQL特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我直接从phpMyadmin插入了大量数据(而不是从PHP页面)。但是当我检索这些数据并在PHP页面中显示它们时,则存在特殊字符的问题。



像 -

 魁北克表演 - 魁北克。 

我的html标题看起来像 -

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Strict // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-strict .dtd> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>

我认为插入数据时可能会发生问题。我怎么能在phpmyadmin中转换这些数据?任何帮助?

解决方案

好像你没有在任何地方使用utf-8 在某个时候搞砸了。取决于你在做什么,你将不得不改变/添加一个或多个以下点(很可能是 SET CHARSET / mysql_set_charset 你忘了):


  • 告诉MySQL使用utf-8。要做到这一点,添加到您的my.cnf:


      collat​​ion_server = utf8_unicode_ci 
    character_set_server = utf8



  • 在与mysql进行交互之前,两个querys:


      SET NAMES'utf8'; 
    CHARSET'utf8';


    或者,让php在打开连接后执行以下操作:


      mysql_set_charset('utf8',$ conn); //当使用mysql_函数时
    mysqli :: set_charset('utf8')//当使用mysqli时


    < blockquote>


  • 将UTF-8设置为数据库的默认字符集
    $ b


      CREATE DATABASE`my_db` DEFAULT CHARACTER SET'utf8'; 



  • 对表格做同样的处理:



    blockquote
    $ pre $ $ $ codeREATE TABLE`my_table`(
    - ...
    )ENGINE = InnoDB DEFAULT CHARSET = utf8;假设客户端是一个浏览器,为您的内容提供服务,



  • 作为utf-8和正确的标题:


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


    要确定浏览器能够理解,请添加元标记:


     < meta http-equiv =Content-typecontent =text / html ; charset = utf-8/> 



  • 最后但并非最不重要的是告诉浏览器使用utf-8提交表单


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




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). but when i retrieve those data and show them in php page, then there are problems with special characters.

like-

Québec shows -  Qu�bec.

my html header looks like -

<!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? any help?

解决方案

seems like you're not using utf-8 everywhere so your data got messed up at some point. 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):

  • tell MySQL to use utf-8. to do this, add this to your my.cnf:

    collation_server = utf8_unicode_ci
    character_set_server = utf8
    

  • before interacting with mysql, send this two querys:

    SET NAMES 'utf8';
    CHARSET 'utf8';
    

    or, alternatively, let php do this after opening the connection:

    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

    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:

    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

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

这篇关于PHP的MySQL特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆