利用php读取数据库时如果页面与数据库编码不一致中文就会有乱码了,但我们可以通过set names来强制统一编码.
首先需要修改文件的类型,我的utf8 提交资料的时候,需要在mysql_query上加这么语句话,代码如下:
mysql_query("SET NAMES 'utf8'",$db);
然后数据库的编码也得是utf8_general_ci,然后在读取的时候也加如下代码:
mysql_query("SET NAMES 'utf8'",$db);
然后就不会实现乱码了,以下代码是是实例:
<?php mysql_query("SET NAMES 'utf8'",$db); $sql = "select * from wd_sql"; $query=mysql_query($sql); $i = 1;//www.phprm.com while ($result = mysql_fetch_array($query)) { $x = $i++; echo "第".$x."位<br>";}
总结一下这个乱码问题的解决方法:
1.确认所有页面都统一使用同样的编码,强烈建议用utf-8;
2. 在用mysql_query作查询时无论是写入数据到mysql还是从mysql提数据出来,设置mysql_query(“set names utf8″)..,连接数据库时就设置,而且不要把这时的“utf8”写成“utf-8”,这点很重要。
教程地址:
欢迎转载!但请带上文章地址^^

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 English version
Recommended: Win version, supports code prompts!
