作者yuffy0327 (鱼排)
看板PHP
标题[请益] 从PHP往SQL捞资料, 无法显示中文
时间Tue Dec 10 18:01:20 2013
以下这是一段来自网路的Sample code, 试过很多种方法都无法显示中文
1. 在PHP里
echo '<meta http-equiv="Content-Type" content="text/html" charset="big5" />';
2. 在PHP外
<meta http-equiv="Content-Type" content="text/html" charset="big5" />
上述方法都试过用utf8来写, 资料库的编码为"utf8_general_ci"
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["name"] = $row["name"];
$product["price"] = $row["price"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.21.79.182
1F:→ yuffy0327:还请各位帮忙了OTZ 12/10 18:02
2F:→ JustGame:资料库是 utf8,但是 charset 却是 big5? 12/10 22:24
3F:→ Bambe:你要不要先用phpmyadmin捞资料出来中文有没有正确显示? 12/10 23:19
4F:→ yuffy0327:回JustGame: 我两种都试过了, 不论是utf8还是big5 12/10 23:41
5F:→ yuffy0327:回Bambe: 我下过Select*, 是可以正常显示中文的 12/10 23:42
6F:→ yuffy0327:在想是不是json_encode的关系, 爬文有看到类似的 12/10 23:45
7F:→ yuffy0327:但试了很久试不出怎麽用, 还麻烦各位为小弟解惑了 12/10 23:45
8F:推 wangpipi:连结资料库时先下query("SET NAMES utf8")?? 12/11 01:31
9F:→ yuffy0327:回wangpipi: 抱歉忘了补我目前写的版本, utf8加了之後 12/11 11:54
10F:→ yuffy0327:出现的是utf8的编码,而不是正常的中文,请问大大有办法 12/11 11:55
11F:→ DiAdo:用javascript把那些utf8编码印出来,应该就可以显示中文了 12/11 12:53
12F:→ MOONRAKER:为什麽非要json_encode() 何不直接print_r()出来看 12/11 15:40