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

PHP mysql 中文乱码

WBOY
WBOYOriginal
2016-06-23 13:40:491166browse

windows mysql 装了SQLyog - 32 bit
INSERT INTO USER VALUES ('123','123','紫轩');
数据库查询发现是乱码:
大概是因为装mysql的时候刚开始的默认格式是latin1,这时建议新建一个数据库(是新数据库不是重装),把编码改成gbk.
解决mysql乱码。
php连接mysql
<?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);//关闭连接 ?>
这里的mysql_query("set names gbk");转变编码。
注意mysql和php这里的编码要统一,页面显示乱码解决

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