Home php教程 php手册 MySQL与PHP产生乱码的具体原因和解决办法介绍

MySQL与PHP产生乱码的具体原因和解决办法介绍

Jun 13, 2016 am 11:07 AM
mysql php and Garbled characters introduce specific Method reason and of solve

MySQL与PHP产生乱码的原因:

◆ MySQL数据库默认的编码是utf8,如果这种编码与你的PHP网页不一致,可能就会造成MySQL乱码;

◆ MySQL中创建表时会让你选择一种编码,如果这种编码与你的网页编码不一致,也可能造成MySQL乱码;

◆ MySQL创建表时添加字段是可以选择编码的,如果这种编码与你的网页编码不一致,也可能造成MySQL乱码;

◆ 用户提交页面的编码与显示数据的页面编码不一致,就肯定会造成PHP页面乱码;

◆ 如用户输入资料的页面是big5码, 显示用户输入的页面却是gb2312,这种100%会造成PHP页面乱码;

◆ PHP页面字符集不正确;

◆ PHP连接MySQL数据库语句指定的编码不正确。

注意:

很多人都怀疑MySQL版本不一致会导致MySQL与PHP产生乱码,相信看了本说明你就不会这样认为了。

平时你在某些网站看到的文字可能有几种编码, 如你看到一个繁体字,它有可能是big5编码,也有 可能是utf-8编码的,更有可能是gb码的,没错,也就是说有简体编码的繁体字,也有繁体编码的简体字,一定要了解这一点。

如果你是做一个简体编码的网页,编码定为GB2312,如果有香港和台湾地区的访客提交繁体的信息,就可能会造成乱码,解决方法:

将网站编码设为 utf-8,这样可以兼容世界上所有字符。

如果网站已经运作了好久,已有很多旧数据,不能再更改简体中文的设定,那么建议将页面的编码设为 GBK, GBK与GB2312的区别就在于:GBK能比GB2312显示更多的字符,要显示简体码的繁体字,就只能用GBK。

使用MySQL与PHP产生乱码的原因都了解得很清楚了,那么解决就不困难了。

MySQL与PHP产生乱码的解决办法:

如果安装MySQL的编码已不能更改,很多朋友是购买虚拟主机建立网站,无权更改MySQL的安装编码,这一关我们可以跳过,因为只要后面的步聚正确,一样能解决乱码问题。

修改数据库编码,如果是数据库编码不正确,可以在PHPmyadmin 执行如下命令:

<ol class="dp-xml"><li class="alt"><span><span>ALTER DATABASE 'test'<br> DEFAULT CHARACTER SET <br>utf8 COLLATE utf8_bin </span></span></li></ol>
Copy after login

以上命令就是将test数据库的编码设为utf8。

修改表的编码:

<ol class="dp-xml"><li class="alt"><span><span>ALTER TABLE 'category' <br>DEFAULT CHARACTER SET <br>utf8 COLLATE utf8_bin </span></span></li></ol>
Copy after login

以上命令就是将一个表category的编码改为utf8。

修改字段的编码:

<ol class="dp-xml"><li class="alt"><span><span>ALTER TABLE 'test'<br> CHANGE 'dd' 'dd' VARCHAR( 45 ) <br>CHARACTER SET utf8 COLLATE utf8_bin NOT NULL </span></span></li></ol>
Copy after login

以上命令就是将test表中dd的字段编码改为utf8。

如果是这种情况容易解决,只需检查下页面,修改源文件的charset即可。

这种情况也是修改页面charset即可。

在连接数据库的语句中。

<ol class="dp-xml">
<li class="alt"><span><span>mysql_connect('localhost','user','password'); </span></span></li>
<li class="alt"><span><span>mysql_select_db('my_db'); </span></span></li>
<li class="alt"><span><span>mysql_query("set names utf8;"); <br>//select 数据库之后加多这一句 </span></span></li>
</ol>
Copy after login

为了避免PHP页面乱码的发生,PHP页面开始第一句

<ol class="dp-xml"><li class="alt"><span><span>header("content-type:text/html; </span><span class="attribute">charset</span><span>=</span><span class="attribute-value">utf</span><span>-8"); <br>//强行指定页面的编码,以避免乱码 </span></span></li></ol>
Copy after login

注意:照以上方法修改以后只能保证你新插入的数据不会造成MySQL与PHP产生乱码,举个例:如果你用户已提交的数据是BIG5,你却想通过以上方法改为可以在GB2312的网页正确显示是不可能的, 这种文字内码的变换只能通过另写程序来解决。


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

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

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.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

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: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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.

See all articles