与Php,Mysql和UTF8有麻烦 [英] Having trouble with Php, Mysql and UTF8

查看:137
本文介绍了与Php,Mysql和UTF8有麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题,简单而烦人。

我只是试图打印从我的mysql数据库收集的名称列表。

Im just trying to print a list of names, collected from my mysql database.

PHP文件保存在utf8中,数据库和表都设置为使用utf8。例如,仍然'å,ä,ö'输出为 。不能相信我还有这个问题。

The PHP-files are saved in utf8, the database and tables are set to use utf8. Still 'å,ä,ö', for example, outputs as �. Can't believe I'm still having this issue.

当然,Latin1解决了这个问题。事情是,我必须使用utf8,因为我做一些json_encode发送数据到ajax脚本。

Of course, Latin1 solves the problem. The thing is that I have to use utf8 since I'm doing some json_encode for sending the data to an ajax-script.

有什么想法可能是错误的?

Any idea what on earth could be wrong?

我应该在返回之前将数据转换为utf8吗?似乎很奇怪,我应该有..

Should I convert the data to utf8 before returning it perhaps? Seems weird I should have to..

推荐答案

utf8_general_ci code> utf8_unicode_ci ...

Convert utf8_general_ci to utf8_unicode_ci...

尝试运行 SET NAMES UTF8 查询

function connect($server, $database, $username, $password, $charset = "UTF8"){
    $link = mysql_connect($server, $database, $password);
    if(!$link){
        die("Unable to connect to database server.");
    }
    mysql_selectdb($database);
    if(function_exists("mysql_set_charset")){
        mysql_set_charset($charset, $link);
    }else{
        mysql_query("SET NAMES $charset");   
    }
}

还要确保你有这个))in your HTML ...

Also make sure you have this (or via header()) in your HTML...

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

这篇关于与Php,Mysql和UTF8有麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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