PHP 中的乱码 UTF-8 字符 [英] Garbled UTF-8 characters in PHP

查看:30
本文介绍了PHP 中的乱码 UTF-8 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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.

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

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

推荐答案

这是因为您没有正确地将编码设置为 UTF-8 (或另一个包含所有要显示的字符).为此,您需要:

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). To do this, you need to:

  • 将编码设置为UTF-8 在数据库中本身(如果您可以在PhpMyAdmin中正确看到文本,则可以跳过此步骤).
  • 告诉 PHP 您正在使用 UTF-8.为此,请运行:

  • Set the encoding to UTF-8 in the database itself (you can skip this step if you can see the text properly in PhpMyAdmin).
  • Tell PHP that you're using UTF-8. To do this, run:

mysql_query("SET NAMES utf8");

在您连接到数据库之后(将此行添加到您的 sql_connect.php 文件的末尾).

right after you connect to the database (add this line to the end of your sql_connect.php file).

请注意,您不应再使用 mysql_* 函数(请参阅我对您的问题的评论).如果你切换到 MySQLi,你可以运行这个:

Please note that you shouldn't use mysql_* functions anymore (see my comment on your question). If you switch to MySQLi, you can run this:

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

更改字符集.

另见:

这篇关于PHP 中的乱码 UTF-8 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆