Home > Backend Development > PHP Tutorial > Solution to Chinese garbled Chinese characters read by PHP

Solution to Chinese garbled Chinese characters read by PHP

WBOY
Release: 2016-07-25 09:12:22
Original
1193 people have browsed it

Example, solve the problem of Chinese garbled characters in the following program.

  1. Data test
  2. $link = mysqli_connect('localhost','root','','happy');
  3. if (!$link ) {
  4. die('Could not connect to MySQL: ' . mysql_error());
  5. }
  6. //Chinese garbled solution - set the default encoding
  7. $link->query("SET NAMES 'UTF8'");
  8. $sql = "select * from subway limit 5";
  9. $result = mysqli_query($link, $sql);
  10. while($row = mysqli_fetch_array($result)){
  11. echo $row['id'] . " : " .$row['code'] . " : " . $row['name'];
  12. echo "
    ";
  13. }
  14. mysqli_close($link);
  15. ?>
Copy the code

before garbled characters: 1:subwayLine1:?? 2: subwayLine1: ??? 3: subwayLine1: ??? 4: subwayLine1: ???? 5: subwayLine1: ???? The result after solving the Chinese garbled characters: 1: subwayLine1: Xinzhuang 2: subwayLine1: outer ring road 3: subwayLine1: Lotus Road 4: subwayLine1: Jinjiang Paradise 5: subwayLine1: Shanghai South Railway Station

The main thing is this sentence: $link->query("SET NAMES 'UTF8'"); Specify default character encoding.



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