PHP / MySQL编码问题。 â 而不是某些字符 [英] PHP/MySQL encoding problems. â�� instead of certain characters

查看:167
本文介绍了PHP / MySQL编码问题。 â 而不是某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用php将特定字符输入到我的mysql数据库时,我遇到了一些问题。我做的是将用户输入的文本提交到数据库。我不知道我需要改变,以允许任何种类的字符被放入数据库,并打印回通过PHP,因为它假设。

I have come across some problems when inputting certain characters into my mysql database using php. What I am doing is submitting user inputted text to a database. I cannot figure out what I need to change to allow any kind of character to be put into the database and printed back out through php as it's suppose to.

我的MySQL排序规则is:latin1_swedish_ci

My MySQL collation is: latin1_swedish_ci

在我从表单发送文本到数据库之前,我对数据使用mysql_real_escape_string()。

Just before I send the text to the database from my form I use mysql_real_escape_string() on the data.

以下示例

此文本:

�People are just as happy as they make up their minds to be.�
� Abraham Lincoln 

看起来像这样:

"People are just as happy as they make up their minds to be."
― Abraham Lincoln


推荐答案

如果你想支持特殊字符,你需要从头到尾转换为UTF8。这意味着你的网页,PHP,mysql连接和mysql表。网页相当简单,只需使用元标记为UTF8。理想情况下,您的标头会说UTF8。

As mentioned by others, you need to convert to UTF8 from end to end if you want to support "special" characters. This means your web page, PHP, mysql connection and mysql table. The web page is fairly simple, just use the meta tag for UTF8. Ideally your headers would say UTF8 also.

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

将您的PHP设置为使用UTF8。事情可能会工作,但是这是一个很好的措施做到这一点:

Set your PHP to use UTF8. Things would probably work anyway, but it's a good measure to do this:

mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');

对于mysql,您希望将表格转换为UTF8,无需导出/导入。 p>

For mysql, you want to convert your table to UTF8, no need to export/import.

ALTER TABLE table_name CONVERT TO CHARACTER SET utf8

你可以,而且应该配置mysql到默认的utf8。但您也可以执行查询:

You can, and should, configure mysql to default utf8. But you can also run the query:

 SET NAMES UTF8

作为建立连接后的第一个查询,并将转换您的数据库连接到UTF8。

as the first query after establishing a connection and that will "convert" your database connection to UTF8.

应该解决你所有的字符显示问题。

That should solve all your character display problems.

这篇关于PHP / MySQL编码问题。 â 而不是某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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