Home > Backend Development > PHP Tutorial > Urgent, urgent, vomiting blood, please help.

Urgent, urgent, vomiting blood, please help.

PHP中文网
Release: 2016-07-25 09:13:34
Original
1019 people have browsed it

php Chinese characters are garbled, Chinese characters are garbled when entering the database

When writing Chinese character data into the database, there will be garbled characters, that is, when you use mysql to query, there will be characters like ???.
The root cause of this situation is that the mysql database is not explicitly told that the data to be inserted is of gbk type.

Solution:
Define the character type when querying:
mysql_query("set names 'gbk'")

The following is a complete example for your reference.

<?php
/*
 @中文字符入库乱码的解决方法
 @m.sbmmt.com
*/
include_once("conn.php");
include_once("include.php");
mysql_query("set names &#39;gbk&#39;")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>";
?>
Copy after login


Related labels:
php
source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template