问题
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!
回答1:
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 = utf8before 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 mysqliset 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" ...>
来源:https://stackoverflow.com/questions/11120774/utf-8-with-mysql-and-php-in-freebsd-swedish-chars-%c3%a5%c3%a4%c3%b6