简体   繁体   English

LAMP UTF-8错误地保存到MySQL数据库

[英]LAMP UTF-8 saving incorrectly to MySQL Database

I've converted my database from Latin 1 to UTF8, and using phpPMyAdmin you can enter data and display it correctly. 我已经将数据库从Latin 1转换为UTF8,并且使用phpPMyAdmin您可以输入数据并正确显示它。 However viewing in the pages I've developed in PHP and editing it using my simple CMS saves characters that must be incorrectly coded. 但是,在我用PHP开发的页面中进行查看并使用简单的CMS对其进行编辑会保存必须正确编码的字符。

I've spent a few hours researching and eventually came up with this code snippet: 我花了几个小时研究,最终想到了以下代码片段:

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn_products);

Which when placed before a DB operation corrects the issue - marvellous! 将其放置在数据库操作之前可以解决此问题-太棒了!

I've done some further research, and have (I believe) discovered a way to make this permanent across an entire MySQL Server, but I would like to permanently change it per database as a majority of the sites using the server are Latin 1, and need to stay that way. 我进行了一些进一步的研究,并且(我相信)发现了一种使它在整个MySQL Server上永久存在的方法,但是我想对每个数据库进行永久更改,因为使用该服务器的大多数网站都是拉丁文1,并且需要保持这种方式。

Can anybody point me in the right direction please? 有人能指出我正确的方向吗?

You cannot do it per-database unfortunately, it's either set by the client application or the server if a client doesn't state anything. 不幸的是,您无法针对每个数据库执行此操作,如果客户端未声明任何内容,则由客户端应用程序或服务器设置。 You only have to set it once per session, so just teaching yourself to let a always follow up a mysql_select_db() that is probably there be with mysql_query('SET NAMES utf8;') (or whatever character set you need) should do the trick. 您只需要在每个会话中设置一次即可,因此只需教给自己一个总是跟进mysql_query('SET NAMES utf8;')可能存在的mysql_select_db() mysql_query('SET NAMES utf8;') (或您需要的任何字符集),特技。

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

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