PHP-MYSQL Chinese garbled problem.
Multi-language support has been introduced since MySQL 4.1, but Chinese characters inserted using PHP will appear garbled. No matter what encoding is used, it will not work.
Solving this problem is actually very simple.
1. When creating the table, set the encoding type to gb2312_chinese_ci.
2. Add a line mysql_query("SET NAMES 'gb2312'",$link); to the database connection statement on the PHP page; for example
$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);
Add this line to both the writing page and the reading page. In this way, the Chinese in MYSQL can be displayed normally.
Related information:
Multi-language support has been introduced since MySQL 4.1, and some features have surpassed other database systems.MySQL 4.1's character set support (Character Set Support) has two aspects: character set (Character Set Support) set) and sorting method (Collation). Support for character sets is refined to four levels: Server, database, table and connection.
To view the system's character set and sorting settings, you can use the following two commands:!
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)
The values listed above are the system default values. (It’s strange how the system defaults to the Swedish sorting method of latin1)...
When we access the MySQL database through PHP in the original way, even if the default character set of the table is set to utf8 and encoded through UTF-8 Send a query and you will find that the data stored in the database is still garbled. The problem lies in this connection layer. The solution is to execute the following sentence before sending the query:
set names 'utf8';
It is equivalent to the following four instructions:
set character_set_client = utf8;
set character_set_results = utf8;
set character_set_connection = utf8;
set collation_connection = utf8_general_ci
by The query submitted on the default web page is gb2312 (can be seen in the form page meta), and mysql treats it as utf8 by default (you can check the character_set_client=utf8), so it must be garbled. In the same way, the result returned by mysql has been converted into Character_set_results is encoded (regardless of the encoding of the table). It also defaults to utf8, and the web page treats it as gb2312, so there must be titles and other data generated by the data. The field read by the library is garbled, but other PHP text is not garbled.
Solution (by Yi Jian Piaoxue):
When installing mysql5.0, you need to select the utf8 character set (you don’t need to select the utf8 character set when creating databases and fields with phpmyadmin), and After php establishes the connection, send
$link = mysql_connect('localhost', 'root', 'root');
mysql_query("SET NAMES 'utf8'",$link);
At this time we What I see on the web page is still garbled, but it is no longer ????. When I check the source file of the web page, it is completely normal. Open the php source file with notepad, don't save it as utf8 encoding, refresh the web page, and everything is done.
Or, of course, you still need to install utf8 when installing. Send set names 'gb2312' in php, and save the php file as Notepad's default ansi, which can also display Chinese correctly.
But always You can't send SET NAMES 'utf8' every time you connect. I haven't found a way to completely solve it.
When installing mysql in this way, the default character set is selected as utf8, which brings another problem. After we enter the mysql console in command.exe, the query results become garbled again. We can enter mysql>set names 'gbk';
or
mysql>set names 'gb2312';
is equivalent to telling the mysql client to use the gb2312 character set, and the result is correct. gb2312 is a sub-child of GBK Set.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
