Home Database Mysql Tutorial MySql 编码设置实际应用代码示例

MySql 编码设置实际应用代码示例

Jun 07, 2016 pm 04:11 PM
mysql code actual application article Example coding set up

以下的文章主要讲述的是MySql 编码设置的实际操作步骤,我们大家都知道MySQL 4.1的实际应用字符集所支持(Character Set Support)主要有2个方面:即,字符集(Character set)与其实际的排序方式(Collation)。 对于实际应用的相关字符集的支持细化到四个层次:

以下的文章主要讲述的是MySql 编码设置的实际操作步骤,我们大家都知道MySQL 4.1的实际应用字符集所支持(Character Set Support)主要有2个方面:即,字符集(Character set)与其实际的排序方式(Collation)。

对于实际应用的相关字符集的支持细化到四个层次:

服务器(server),数据库(database),数据表(table)和连接(connection)。

查看系统的字符集和排序方式的设定可以通过下面的两条命令或mysql> status 。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<ol class="dp-xml">

<li class="alt"><span><span>mysql</span><span class="tag">></span><span> SHOW VARIABLES LIKE 'character_set_%';   </span></span></li>

<li><span>+--------------------------+----------------------------+   </span></li>

<li class="alt"><span>| Variable_name | Value |   </span></li>

<li><span>+--------------------------+----------------------------+   </span></li>

<li class="alt"><span>| character_set_client | latin1 |   </span></li>

<li><span>| character_set_connection | latin1 |   </span></li>

<li class="alt"><span>| character_set_database | latin1 |   </span></li>

<li><span>| character_set_results | latin1 |   </span></li>

<li class="alt"><span>| character_set_server | latin1 |   </span></li>

<li><span>| character_set_system | utf8 |   </span></li>

<li class="alt"><span>| character_sets_dir | /usr/share/mysql/charsets/ |   </span></li>

<li><span>+--------------------------+----------------------------+   </span></li>

<li class="alt"><span>7 rows in set (0.00 sec)   </span></li>

<li><span> </span></li>

<li class="alt">

<span>mysql</span><span class="tag">></span><span> SHOW VARIABLES LIKE 'collation_%';   </span>

</li>

<li><span>+----------------------+-------------------+   </span></li>

<li class="alt"><span>| Variable_name | Value |   </span></li>

<li><span>+----------------------+-------------------+   </span></li>

<li class="alt"><span>| collation_connection | latin1_swedish_ci |   </span></li>

<li><span>| collation_database | latin1_swedish_ci |   </span></li>

<li class="alt"><span>| collation_server | latin1_swedish_ci |   </span></li>

<li><span>+----------------------+-------------------+   </span></li>

<li class="alt"><span>3 rows in set (0.00 sec)  </span></li>

</ol>

Copy after login

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

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

1.SET NAMES 'utf8';

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

1

2

3

4

5

6

7

8

9

<ol class="dp-xml">

<li class="alt"><span><span>SET </span><span class="attribute">character_set_client</span><span> = </span><span class="attribute-value">utf8</span><span>;   </span></span></li>

<li>

<span>SET </span><span class="attribute">character_set_results</span><span> = </span><span class="attribute-value">utf8</span><span>;   </span>

</li>

<li class="alt">

<span>SET </span><span class="attribute">character_set_connection</span><span> = </span><span class="attribute-value">utf8</span><span>;  </span>

</li>

</ol>

Copy after login

2. 创建数据库

1

<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> create database name character set utf8;  </span></span></li></ol>

Copy after login

3. 创建表

1

2

3

4

5

6

7

8

9

10

11

<ol class="dp-xml">

<li class="alt"><span><span>CREATE TABLE `type` (   </span></span></li>

<li><span>`id` int(10) unsigned NOT NULL auto_increment,   </span></li>

<li class="alt"><span>`flag_deleted` enum('Y','N') character set utf8 NOT NULL default 'N',   </span></li>

<li><span>`flag_type` int(5) NOT NULL default '0',   </span></li>

<li class="alt"><span>`type_name` varchar(50) character set utf8 NOT NULL default '',   </span></li>

<li><span>PRIMARY KEY (`id`)   </span></li>

<li class="alt">

<span>) DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>;  </span>

</li>

</ol>

Copy after login

4. 修改数据库成utf8的.

1

<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> alter database name character set utf8;  </span></span></li></ol>

Copy after login

5. 修改表默认用utf8.

1

<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> alter table type character set utf8;  </span></span></li></ol>

Copy after login

6. 修改字段用utf8

 

1

<ol class="dp-xml"><li class="alt"><span><span>mysql</span><span class="tag">></span><span> alter table type modify type_name varchar(50) CHARACTER SET utf8;  </span></span></li></ol>

Copy after login

以上的相关内容就是对MySql 编码设置的介绍,望你能有所收获。


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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

mysql whether to change table lock table mysql whether to change table lock table Apr 08, 2025 pm 05:06 PM

When MySQL modifys table structure, metadata locks are usually used, which may cause the table to be locked. To reduce the impact of locks, the following measures can be taken: 1. Keep tables available with online DDL; 2. Perform complex modifications in batches; 3. Operate during small or off-peak periods; 4. Use PT-OSC tools to achieve finer control.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

The primary key of mysql can be null The primary key of mysql can be null Apr 08, 2025 pm 03:03 PM

The MySQL primary key cannot be empty because the primary key is a key attribute that uniquely identifies each row in the database. If the primary key can be empty, the record cannot be uniquely identifies, which will lead to data confusion. When using self-incremental integer columns or UUIDs as primary keys, you should consider factors such as efficiency and space occupancy and choose an appropriate solution.

Can mysql handle multiple connections Can mysql handle multiple connections Apr 08, 2025 pm 03:51 PM

MySQL can handle multiple concurrent connections and use multi-threading/multi-processing to assign independent execution environments to each client request to ensure that they are not disturbed. However, the number of concurrent connections is affected by system resources, MySQL configuration, query performance, storage engine and network environment. Optimization requires consideration of many factors such as code level (writing efficient SQL), configuration level (adjusting max_connections), hardware level (improving server configuration).

Can mysql run on android Can mysql run on android Apr 08, 2025 pm 05:03 PM

MySQL cannot run directly on Android, but it can be implemented indirectly by using the following methods: using the lightweight database SQLite, which is built on the Android system, does not require a separate server, and has a small resource usage, which is very suitable for mobile device applications. Remotely connect to the MySQL server and connect to the MySQL database on the remote server through the network for data reading and writing, but there are disadvantages such as strong network dependencies, security issues and server costs.

Can mysql return json Can mysql return json Apr 08, 2025 pm 03:09 PM

MySQL can return JSON data. The JSON_EXTRACT function extracts field values. For complex queries, you can consider using the WHERE clause to filter JSON data, but pay attention to its performance impact. MySQL's support for JSON is constantly increasing, and it is recommended to pay attention to the latest version and features.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

See all articles