php 中文字符入库或显示乱码问题的解决方法

这个的问题就出在在php里没有告诉mysql数据库你要插入的数据是gbk类型的,要解决其实很简单。连接数据库后加上这么一句话就OK了。

大家以后在编写过程中, 一定要记得定义字符类型。
mysql_query("set names 'gbk'")
解决的方法就这么简单。
今天做了一个数据库查询,放出代码。

复制代码 代码如下:


<?php
/*
filename:query.php
do:get and show the data
author:
*/
include_once("conn.php");
include_once("include.php");
mysql_query("set names 'gbk'")or die("设置字符库失败\n");
mysql_select_db($db)or die("连接数据库失败!\n");
$exec = "select * from $table";
//echo $exec;
$result = mysql_query($exec,$conn)or die("查询数据库失败\n");
echo "<table border=2>";
for($cout=0;$cout<mysql_numrows($result);$cout++)
{
$city = mysql_result($result,$cout,city);
$name = mysql_result($result,$cout,name);
$phone = mysql_result($result,$cout,phone);
echo "<tr>";
echo "city: $city";
echo "name: $name";
echo "phone: $phone";
echo "</tr>";
}
echo "</table>";
?>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/b566293c415b015d9079cc0ae2ef3797.html