Table of Contents
PHP+MYSQL中文乱码问题
Home php教程 php手册 PHP+MYSQL中文乱码问题

PHP+MYSQL中文乱码问题

Jun 13, 2016 am 08:59 AM
php+mysql Chinese main Garbled characters several kinds article Summary solve question

PHP+MYSQL中文乱码问题

   这篇文章主要汇总介绍了几种解决PHP+MYSQL中文乱码问题的方法,十分的实用,有需要的小伙伴可以参考下。

  从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行.

  解决这个问题其实很简单.

  1.在建表的时候设置编码类型为gb2312_chinese_ci.

  2.在PHP页面的数据库连接语句加一行mysql_query("SET NAMES 'gb2312'",$link); 例如

  ?

1

2

3

4

5

6

7

8

9

10

$db_host="localhost";

$db_user="root";

$db_password="password";

$db_name="test";

 

$link=mysql_connect($db_host,$db_user,$db_password);

mysql_query("SET NAMES 'gb2312'",$link);

$db=mysql_select_db($db_name,$link);

$query="select * from user";

$result=mysql_query($query);

  写入页面和读取页面都加入这行.这样在MYSQL里面的中文就能正常显示了.

  相关资料:

  从MySQL 4.1开始引入多语言的支持,而且一些特性已经超过了其他的数据库系统。

  MySQL4.1的字符集支持(Character Set Support)有两个方面:字符集(Characterset)和排序方式(Collation)。对于字符集的支持细化到四个层次:服务器 (server),数据库(database),数据表(table)和连接(connection)。

  查看系统的字符集和排序方式的设定可以通过下面的两条命令:!

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

mysql> show variables like 'character_set_%';

+--------------------------+----------------------------+

| Variable_name | Value |

+--------------------------+----------------------------+

| character_set_client | latin1 |

| character_set_connection | latin1 |

| character_set_database | latin1 |

| character_set_results | latin1 |

| character_set_server | latin1 |

| character_set_system | utf8 |

| character_sets_dir | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

7 rows in set (0.00 sec)

mysql> show variables like 'collation_%';

+----------------------+-------------------+

| Variable_name | Value |

+----------------------+-------------------+

| collation_connection | latin1_swedish_ci |

| collation_database | latin1_swedish_ci |

| collation_server | latin1_swedish_ci |

+----------------------+-------------------+

3 rows in set (0.00 sec)

  上面列出的值就是系统的默认值。(很奇怪系统怎么默认是latin1的瑞典语排序方式)...

  当我们按照原来的方式通过PHP存取MySQL数据库时,就算设置了表的默认字符集为utf8并且通过UTF-8编码发送查询,你会发现存入数据库的仍然是乱码。问题就出在这个connection连接层上。解决方法是在发送查询前执行一下下面这句:

  set names 'utf8';

  它相当于下面的四句指令:

  代码如下:

  set character_set_client = utf8;

  set character_set_results = utf8;

  set character_set_connection = utf8;

  set collation_connection = utf8_general_ci

  由于默认网页提交的查询是gb2312(表单页面meta里可以看到),而mysql默认将其当作utf8(可以查到此时的 character_set_client=utf8),所以必然乱码。同理,mysql返回的结果是已经转换成 character_set_results编码的(与表的编码无关),同样默认是utf8,而网页页面把它当gb2312处理,所以必然有标题等由数据 库读出的字段是乱码而其他php部分文字不乱码的现象。

  解决(by 一剑飘雪):

  安装mysql5.0时要选utf8字符集(在用phpmyadmin创建数据库和字段时就不需要在整理中选utf8字符集了),并在php建立连接后发送

  复制代码 代码如下:

  $link = mysql_connect('localhost', 'root', 'root');

  mysql_query("SET NAMES 'utf8'",$link);

  这时我们在网页中看到的还是乱码但已不是????了,查看网页源文件,已完全正常。用记事本打开php源文件,别存为utf8编码,再刷新网页,全部搞定了。

  或者,当然还是要安装时仍要utf8安装,在php中发送set names 'gb2312',同时php文件存为记事本默认的ansi,也能正确显示中文.

  但总不能每次连接时都发送一次SET NAMES 'utf8'吧,如何彻底解决还没找到方法。

  这样安装mysql时缺省字符集选为utf8后又带来一个问题,我们在command.exe中进入mysql控制台后,查询结果又成了乱码,我们可以在查询前输入

   代码如下:

  mysql>set names 'gbk';

  或

  复制代码 代码如下:

  mysql>set names 'gb2312';

  相当于告诉mysql客户端在使用gb2312字符集,结果就正确了,gb2312为GBK的子集.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! How can I make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! Mar 15, 2024 pm 04:13 PM

1. How can you make money by publishing articles on Toutiao today? How to earn more income by publishing articles on Toutiao today! 1. Activate basic rights and interests: original articles can earn profits by advertising, and videos must be original in horizontal screen mode to earn profits. 2. Activate the rights of 100 fans: if the number of fans reaches 100 fans or above, you can get profits from micro headlines, original Q&A creation and Q&A. 3. Insist on original works: Original works include articles, micro headlines, questions, etc., and are required to be more than 300 words. Please note that if illegally plagiarized works are published as original works, credit points will be deducted, and even any profits will be deducted. 4. Verticality: When writing articles in professional fields, you cannot write articles across fields at will. You will not get appropriate recommendations, you will not be able to achieve the professionalism and refinement of your work, and it will be difficult to attract fans and readers. 5. Activity: high activity,

Tips for solving Chinese garbled characters when writing txt files with PHP Tips for solving Chinese garbled characters when writing txt files with PHP Mar 27, 2024 pm 01:18 PM

Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

Black Shark mobile phone charging troubleshooting and solutions Black Shark mobile phone charging troubleshooting and solutions Mar 22, 2024 pm 09:03 PM

Black Shark is a smartphone brand known for its powerful performance and excellent gaming experience. It is loved by gamers and technology enthusiasts. However, just like other smartphones, Black Shark phones will have various problems, among which charging failure is a common one. Charging failure will not only affect the normal use of the mobile phone, but may also cause more serious problems, so it is very important to solve the charging problem in time. This article will start with the common causes of Black Shark mobile phone charging failures and introduce methods to troubleshoot and solve charging problems. I hope it can help readers solve the problem of Black Shark mobile phones.

See all articles