Home > Backend Development > PHP Tutorial > PHP mysql 中文乱码

PHP mysql 中文乱码

WBOY
Release: 2016-06-23 13:40:49
Original
1184 people have browsed it

windows mysql 装了SQLyog - 32 bit
Copy after login
INSERT INTO USER VALUES ('123','123','紫轩');
Copy after login
数据库查询发现是乱码:
Copy after login
大概是因为装mysql的时候刚开始的默认格式是latin1,这时建议新建一个数据库(是新数据库不是重装),把编码改成gbk.
Copy after login
解决mysql乱码。
Copy after login
php连接mysql
Copy after login
<?php  $db_host='localhost';  $db_database='work';  $db_username='root';  $db_password='yubing';  $connection=mysql_connect($db_host,$db_username,$db_password);//连接到数据库  mysql_query("set names 'gbk'");//编码转化  if(!$connection)  {    die("could not connect to the database:</br>".mysql_error());//诊断连接错误  }  $db_selecct=mysql_select_db($db_database);//选择数据库  if(!$db_selecct)  {    die("could not to the database</br>".mysql_error());   }  $query="select * from user where username = '$user_name'";//构建查询语句  $result=mysql_query($query);//执行查询  if(!$result)  {    die("could not to the database</br>".mysql_error());  }// array mysql_fetch_row(resource $result);  while($result_row=mysql_fetch_row(($result)))//取出结果并显示  {  $username=$result_row[0];  $password=$result_row[1];  $chname=$result_row[2];  echo "<tr>";  echo "<td>$username</td>";echo "<tr>";  echo "<td>$password</td>";echo "<tr>";  echo "<td>$chname</td>";echo "<tr>";  echo "</tr>";  }  mysql_close($connection);//关闭连接 ?>
Copy after login
这里的mysql_query("set names gbk");转变编码。
Copy after login
注意mysql和php这里的编码要统一,页面显示乱码解决
Copy after login

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template