search
Homephp教程php手册PHP连接mysql数据库乱码解决办法

php连接mysql中文查询乱码问题解决非常的简单的,因为这个是我们文档编码与数据库编码不统一所导致的,解决办法有许多下面来给各位总结一下.

测试环境,mysql编码为uft-8,mysql版本为mysql5.X,php版本为php 5.X

解决办法:

1.确保HTML代码中声明的charset值等于utf-8,代码如下:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>

1:确认phpmyadmin用的mysql字符集为utf-8 unicode(utf8);mysql连接校对为utf8_general_ci,language为中文-chinese simplified

2:确认每个数据表的"整理为"utf8_general_ci;

3:在php连接mysql数据库后加个语句来指定数据库的字符集,例如,代码如下:

$conn=mysql_connect("localhost","root","你的密码"); //连接数据库
mysql_query("set name &#39;utf8&#39;");

如果上面解决不了我们可如下操作,代码如下:

$link = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;);  
mysql_query("set character_set_connetion=utf8,character_set_result=utf8, character_set_client=binary",$link);

最后找到一个相对比较全面的解决办法,后来找到了解决方案,记录如下.

尝试创建数据库表:

mysql_select_db("courseTable",$con); 
mysql_query("SET NAMES &#39;utf8&#39;"); 
mysql_query("SET CHARACTER_SET_CLIENT=utf8"); 
mysql_query("SET CHARACTER_SET_RESULTS=utf8");

总结,关于mysql中文乱码我是找到很多的办法但最好发现网上很多解决mysql乱码都掉JB蛋的,真正的只要在查询连接时加上mysql_query("SET NAMES 'utf8'");就可以解决了.


教程地址:

欢迎转载!但请带上文章地址^^

Statement
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

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment