Home Backend Development PHP Tutorial PHP connection to MySQL query results showing garbled Chinese characters solution_PHP tutorial

PHP connection to MySQL query results showing garbled Chinese characters solution_PHP tutorial

Jul 13, 2016 am 10:25 AM
mysql php Garbled characters

We first assume that the encoding used in the database is UTF-8
At this time, we should first add

Copy code to the PHP page. The code is as follows :



charset here The value of utf-8 must be the same as the encoding type when the file is saved After

, add

before the database query. Copy the code . The code is as follows:

mysql_query("set names 'utf8'" );

The encoding value of this line of statements should also be the same as the encoding value above.

In short, the encoding type saved in the web page, the charset=utf-8 of the web page, and the encoding method of the set names utf8 statement executed should be consistent

The following is a good analysis

MySQL "SET NAMES x" character set problem analysis

Recently received training from BBT to build a voting system. The system code was not difficult, but my time was mainly spent studying character sets and encodings. The encoding (character set) issues of the MySQL and Apache systems made me rack my brains and suffer a lot. The solutions to these problems on the Internet are scattered and one-sided. Most of them provide solutions without explaining why. So I will summarize what I have gained in the past few days to avoid latecomers from taking detours again. This article is a little helpful for writing PHP (after reading it, you will know how to make your PHP program display normally on the servers of most space providers), but more help lies in the establishment and settings of the network server.

Let’s talk about MySQL’s character set first. Under Windows, you can modify the

in my.ini

1.# CLIENT SECTION
2.[mysql]
3.default-character-set=utf8
4.# SERVER SECTION
5.[mysqld]
6.default -character-set=utf8

These two fields are used to change the default character set of the database. The first is the client's default character set, and the second is the server's default character set. Suppose we set both to utf8, and then enter "show variebles like "character_set_%";" in the MySQL Command Line Client, you can see the following characters:

character_set_client latin1
character_set_connection latin1
character_set_database utf8
character_set_results latin1
character_set_server utf8
character_set_system utf8

The utf8 in it changes with our settings above. At this time, if we read data from the database through a PHP program using UTF-8, it is likely to be a string of "?????" or other garbled characters. After searching online for a long time, the solution is simple. After connecting to the database and before reading the data, first execute a query "SET NAMES UTF8", which is

in PHP

1.mysql_query("SET NAMES UTF8");

will display normally (as long as the characters of the information in the database are normal). Why is this happening? What exactly does the query "SET NAMES UTF8" do?

Enter "SET NAMES UTF8;" on the MySQL command line, and then execute "show variebles like "character_set_%";" and find that the values ​​of the variables "character_set_client", "character_set_connection", and "character_set_results" that were originally latin1 have all changed. It's utf8. It turns out that these three variables are causing trouble. Check the manual, the above sentence is equal to:

1.SET character_set_client = utf8;
2.SET character_set_results = utf8;
3.SET character_set_connection = utf8;

Look at the functions of these three variables:

Information input path: client→connection→server;
Information output path: server→connection→results.

In other words, each path needs to change the character set encoding 3 times. Take the garbled output as an example. For the utf8 data in the server, the incoming connection is converted to latin1, the incoming results are converted to latin1, and the utf-8 page converts the results again. If the two character sets are incompatible, such as latin1 and utf8, the conversion process is irreversible and destructive. So it can't be turned back.

But it should be stated here that the effect of "SET NAMES UTF8" is only temporary, and MySQL will return to the default after restarting.

The next step is to talk about the configuration of MySQL on the server. Don't we have to add "SET NAMES UTF8" every time we read and write to the database to ensure that the encoding of data transmission is consistent? Can I configure MySQL so that the three variables default to the character set we want? The manual doesn't say it, and I couldn't find the answer online. Therefore, from a server configuration perspective, there is no way to omit that line of code.

Summary: In order to allow your webpage to be displayed normally on more servers, add "SET NAMES UTF8", even if you have not added it now Sentences can also be accessed normally.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824862.htmlTechArticleWe first assume that the encoding used in the database is UTF-8. At this time, we should first add the copy code to the PHP page The code is as follows: meta http-equiv="Content-Type" content="text/html; ch...
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

See all articles