PHP項目出現亂碼,緣由就是軟件之間,模板之間的編碼有差別,不相同:javascript
mysql_query("set character set 'utf8'"); //讀庫php
mysql_query("set names 'utf8'"); //寫庫css
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> //html頁面中使用的編碼html
示例代碼1:
$mysqli=new mysqli("localhost", "mysql_user", "mysql_pwd", "mydb"); //創建mysqli對象
//執行SQL語句從用戶表User中查詢全部記錄,保存在$User數組中
$mysqli->query("set names gb2312");
if($result=$mysqli->query("SELECT id, name, sex, age, email FROM User")){
while($row=$result->fetch_assoc()){ //循環從結果集中遍歷每行數據
$users[]=$row; //取出全部行都保存在同一個數組中
}
$rowNum=$result->num_rows; //將獲取的數據行數保存在變量中
$result->close(); //關閉結果集
}
$mysqli->close(); java
示例代碼2:mysql
<?php $charset = "utf8"; $con = mysql_connect("localhost", "root", ""); mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $con); mysql_select_db("ecshop", $con); $sql = "SELECT user_name, email FROM ecs_admin_user WHERE user_id = 4"; $result = mysql_query($sql, $con); $array = mysql_fetch_array($result, MYSQL_ASSOC); mysql_close($con); $name = $array["user_name"]; $email = $array["email"]; ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <h1>你好, <?php echo $name; ?>!</h1> <h2>你的郵件是:<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a></h2> </body> </html>
補充:
# Lang-abbv Charset Language
#---------------------------------
en ISO-8859-1 English
UTF-8 utf8 UTF-8
Unicode ucs Unicode
th Cp874 Thai
ja SJIS Japanese
ko Cp949 Korean
zh Cp950 Chinese-Traditional
zh-cn GB2312 Chinese-Simplified
zh-tw Cp950 Chinese
cs ISO-8859-2 Czech
hu ISO-8859-2 Hungarian
hr ISO-8859-2 Croation
pl ISO-8859-2 Polish
ro ISO-8859-2 Romanian
sr ISO-8859-2 Serbian
sk ISO-8859-2 Slovak
sl ISO-8859-2 Slovenian
sq ISO-8859-2 Albanian
bg ISO-8859-5 Bulgarian
be ISO-8859-5 Byelorussian
mk ISO-8859-5 Macedonian
ru ISO-8859-5 Russian
uk ISO-8859-5 Ukrainian
ca ISO-8859-1 Catalan
de ISO-8859-1 German
da ISO-8859-1 Danish
fi ISO-8859-1 Finnish
fr ISO-8859-1 French
es ISO-8859-1 Spanish
is ISO-8859-1 Icelandic
it ISO-8859-1 Italian
nl ISO-8859-1 Dutch
no ISO-8859-1 Norwegian
pt ISO-8859-1 Portuguese
sv ISO-8859-1 Swedish
af ISO-8859-1 Afrikaans
eu ISO-8859-1 Basque
fo ISO-8859-1 Faroese
gl ISO-8859-1 Galician
ga ISO-8859-1 Irish
gd ISO-8859-1 Scottish
mt ISO-8859-3 Maltese
eo ISO-8859-3 Esperanto
el ISO-8859-7 Greek
tr ISO-8859-9 Turkish
he ISO-8859-8 Hebrew
iw ISO-8859-8 Hebrew
ar ISO-8859-6 Arabic
et ISO-8859-1 Estonian
lv ISO-8859-2 Latvian
lt ISO-8859-2 Lithuanian
sql