简体   繁体   English

PHP中的UTF-8字符乱码

[英]Garbled UTF-8 characters in PHP

<?php
        require ('sql_connect.php');

        $query = "select * from `products`";
        $result = mysql_query($query);

        while($row = mysql_fetch_array($result))
        {
            $imagem = $row['imagem'];
            $texto = $row['texto'];

            echo "<img src=Images/Products/$imagem> <br> $texto";
        }
        ?>    

I have the image and the text of the image that i want to show in my database. 我有要显示在数据库中的图像和图像文本。

I show the image but i have problems with the text... The text dont show the enters/spaces and the accents. 我显示图像,但是文本有问题...文本不显示输入/空格和重音符号。

Image of problem: http://imgur.com/LHg4eK6,C7SZD4M 问题图片: http//imgur.com/LHg4eK6,C7SZD4M

I want this: http://imgur.com/LHg4eK6,C7SZD4M#1 我想要这个: http : //imgur.com/LHg4eK6,C7SZD4M#1

This is happening because you didn't properly set the encoding to UTF-8 (or another one that contains all characters you want to display) . 发生这种情况是因为您未将编码正确设置为UTF-8 (或另一种包含要显示的所有字符的编码 To do this, you need to: 为此,您需要:

  • Set the encoding to UTF-8 in the database itself (you can skip this step if you can see the text properly in PhpMyAdmin). 在数据库本身中将编码设置UTF-8 (如果可以在PhpMyAdmin中正确看到文本,则可以跳过此步骤)。
  • Tell PHP that you're using UTF-8 . 告诉PHP您正在使用UTF-8 To do this, run: 为此,请运行:

     mysql_query("SET NAMES utf8"); 

    right after you connect to the database (add this line to the end of your sql_connect.php file) . 在您连接到数据库之后(将此行添加到sql_connect.php文件的末尾)


Please note that you shouldn't use mysql_* functions anymore (see my comment on your question) . 请注意,您不应再使用mysql_*函数(请参阅我对问题的评论) If you switch to MySQLi, you can run this: 如果切换到MySQLi,则可以运行以下命令:

$db->set_charset("utf-8");

to change the charset. 更改字符集。


See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM