UTF-8与mysql和php在freebsd swedish chars(åäö) [英] UTF-8 with mysql and php in freebsd swedish chars (åäö)

查看:172
本文介绍了UTF-8与mysql和php在freebsd swedish chars(åäö)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

UTF-8一直通过


Hi我最近在我的服务器上从linux切换到freebsb,现在我的数据库正在起作用。



当我尝试回显一个包含å,ä或ö(瑞典字母)的字符串)它成为一个问号。即:Söndag成为S ndag。
echoä;作品。
$ file --mime test.php
test.php:text / plain; charset = utf-8



test.php

 <?php 
$ a =å;
mysql_connect(localhost,root,:-));
mysql_select_db(lidev);

$ result = mysql_query(select * from DLG where dag ='Onsdag');
$ row = mysql_fetch_array($ result);

echo $ row ['dagens'];
echo< br>;
echo mb_detect_encoding($ row ['dagens']);
?>

mb_detect_encoding()输出UTF-8
我的mysql表位于 utf8_general_ci



我完全卡住了!怎么了?



干杯!

解决方案

不要在任何地方使用utf-8,所以你的数据在某些时候搞砸了。取决于您正在做什么,您必须更改/添加以下一个或多个点(很可能是 SET CHARSET / mysql_set_charset 你忘了):




  • 告诉MySQL使用utf-8。这样做,添加到你的my.cnf:


      collat​​ion_server = utf8_unicode_ci 
    在与mysql进行交互之前,请先发送这个字符集,然后再将其发送给两个查询:


      SET NAMES'utf8'; 
    CHARSET'utf8';


    或者,打开连接后让PHP执行此操作:


      mysql_set_charset('utf8',$ conn); //当使用mysql_-functions 
    mysqli :: set_charset('utf8')//当使用mysqli


    < blockquote>


  • 将UTF-8设置为数据库的默认字符集


      CREATE DATABASE`my_db` DEFAULT CHARACTER SET'utf8'; 



  • 对表执行相同操作:


      CREATE TABLE`my_table`(
    - ...
    )ENGINE = InnoDB DEFAULT CHARSET = utf8;



  • 假设客户端是浏览器,为您的内容提供服务作为utf-8和正确的标题:


      header('Content-type:text / html; charset = utf-8'); 


    真的确保浏览器了解,添加元标记:


     < meta http-equiv =Content-typecontent =text / html ; charset = utf-8/> 



  • 最后但并非最不重要的是告诉浏览器使用utf-8提交表单


     < form accept-charset =utf-8 ...> 




Possible Duplicate:
UTF-8 all the way through

Hi I recently switched from linux to freebsb on my server, and now my database is acting up.

When I try to echo a string containing å,ä or ö (swedish letters) it becomes a questionmark. ie: Söndag becomes S�ndag. echo "ä"; works. $ file --mime test.php test.php: text/plain; charset=utf-8

test.php

<?php
$a="å";
mysql_connect("localhost", "root", ":-)");
mysql_select_db("lidev");

$result=mysql_query("select * from DLG where dag='Onsdag'");
$row=mysql_fetch_array($result);

echo $row['dagens'];
echo "<br>";
echo mb_detect_encoding($row['dagens']);
?>

mb_detect_encoding() outputs "UTF-8" My mysql table is in utf8_general_ci.

I'm completely stuck! What's wrong?

Cheers!

解决方案

seems like you're not using utf-8 everywhere so your data got messed up at some point. depending on what exactly you're doing, you'll have to change/add one or more of the following points (most likely it's the SET CHARSET/mysql_set_charset you forgot):

  • tell MySQL to use utf-8. to do this, add this to your my.cnf:

    collation_server = utf8_unicode_ci
    character_set_server = utf8
    

  • before interacting with mysql, send this two querys:

    SET NAMES 'utf8';
    CHARSET 'utf8';
    

    or, alternatively, let php do this after opening the connection:

    mysql_set_charset('utf8', $conn); // when using the mysql_-functions
    mysqli::set_charset('utf8') // when using mysqli
    

  • set UTF-8 as the default charset for your database

    CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8';
    

  • do the same for tables:

    CREATE TABLE `my_table` (
      -- ...
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

  • assuming the client is a browser, serve your content as utf-8 and the the correct header:

    header('Content-type: text/html; charset=utf-8');
    

    to be really sure the browser understands, add a meta-tag:

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

  • and, last but not least, tell the browser to submit forms using utf-8

    <form accept-charset="utf-8" ...>
    

这篇关于UTF-8与mysql和php在freebsd swedish chars(åäö)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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