PHP+MYSQL中文乱码问题_php技巧
从MySQL 4.1开始引入多语言的支持,但是用PHP插入的中文会出现乱码.无论用什么编码也不行.
解决这个问题其实很简单.
1.在建表的时候设置编码类型为gb2312_chinese_ci.
2.在PHP页面的数据库连接语句加一行mysql_query("SET NAMES 'gb2312'",$link); 例如
$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)。
查看系统的字符集和排序方式的设定可以通过下面的两条命令:!
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的子集.

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 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.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

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 is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
