当我从mySQL数据库返回一行时,我得到一个?而不是一些字符,例如:ò,à 我的mysql行和表设置为utf8_unicode_ci,所以我认为数据库正确存储它但是php没有正确地返回它。
认为它与mysql_set_charset
有关,但无法让它正常工作。任何帮助将不胜感激!!
<?php
if($row = mysql_fetch_assoc(queryDb("SELECT * FROM customer WHERE uuid='".$_COOKIE['uuid']."'")))
{
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$gender = $row['gender'];
$ileach_first_name = $row['ileach_first'];
$ileach_last_name = $row['ileach_last'];
}
//If Ileach Name is blank
if($ileach_last_name == "" || $ileach_first_name == ""){
// Get ileach last name
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_last_names WHERE eng_name='$last_name'"));
$ileach_last_name = $row['gae_name'];
if($ileach_last_name == "") {
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_last_names order by rand() limit 1"));
$ileach_last_name = $row['gae_name'];}
//Get ileach First Name
//If Male
if($gender == 'M') {
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_first_names_m WHERE eng_name='$first_name'"));
$ileach_first_name = $row['gae_name'];
//If no name is selected, get one randomly
if($ileach_first_name == "") {
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_first_names_m order by rand() limit 1"));
$ileach_first_name = $row['gae_name']; }
}
//If Female
else{
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_first_names_f WHERE eng_name='$first_name'"));
$ileach_first_name = $row['gae_name'];
//If no name is selected, get one randomly
if($ileach_first_name == "") {
$row = mysql_fetch_assoc(queryDb("SELECT * FROM ileach_first_names_f order by rand() limit 1"));
$ileach_first_name = $row['gae_name'];}
}
//Save ileach name into db
mysql_query("UPDATE customer SET ileach_first = '$ileach_first_name'
WHERE uuid='".$_COOKIE['uuid']."' ");
mysql_query("UPDATE customer SET ileach_last = '$ileach_last_name'
WHERE uuid='".$_COOKIE['uuid']."' ");
}
//Stitch name together.
$full_ileach_name .=$ileach_first_name;
$full_ileach_name .= " ";
$full_ileach_name .= $ileach_last_name;
?>
答案 0 :(得分:10)
“认为它与mysql_set_charset有关” - 是的,非常可能。
$mysql = mysql_connect(...); // TODO: error handling
mysql_select_db('...', $mysql); // TODO: error handling
mysql_set_charset('utf8', $mysql); // TODO: error handling
对于详细信息,我们需要了解有关函数queryDB()和相关内容的更多信息(例如,建立数据库连接的位置)
另见:
答案 1 :(得分:5)
尝试将其放在mysql_select_db:
之后mysql_query ( "set character_set_client='utf8'" );
mysql_query ( "set character_set_results='utf8'" );
mysql_query ( "set collation_connection='utf8_unicode_ci'" );
答案 2 :(得分:0)
发送第一个查询:
SET NAMES 'utf8'
应该工作;)
MySQL正确存储文本,PHP正确格式化,但问题在于连接字符集:)
像@VolkerK建议的那样,使用mysql_set_charset func
答案 3 :(得分:0)
文件编码本身是否正确?
您是否尝试过使用utf8_encode或utf8_decode? 见:http://php.net/manual/en/function.utf8-encode.php
如果utf8_encode有效,我会假设数据库的结果是错误的字符集。
如果utf8_decode有效,我会假设该文件有错误的字符集,或者浏览器可能在错误的字符集中解析它。 这可以通过标题修复:
header('Content-Type: text/html; charset=utf-8');
或在&lt; head&gt;:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
请参阅:http://tlt.its.psu.edu/suggestions/international/web/tips/declare.html