PHP:有特殊字符时为空(Ñ...)

时间:2013-05-22 08:02:59

标签: php encoding

我有一个PHP脚本和数据库中的表。 php脚本选择所有行并将它们带到一个数组。我还有一个调用此脚本的Android设备。一切正常,但如果数据库行中有一些特殊字符(如“Ñ”),脚本将返回NULL值。我该如何解决这个问题? 这是代码:

 $response = array();

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();

$result = mysql_query("SELECT *FROM candi") or die(mysql_error());


if (mysql_num_rows($result) > 0) {

$response["candi"] = array();

while ($row = mysql_fetch_array($result)) {

$candi = array();

$candi["id"] = $row["id"];
$candi["name"] = $row["name"];



array_push($response["candi"], $candi);
}
$response["success"] = 1;

1 个答案:

答案 0 :(得分:1)

走出困境并假设你json_encode值:json_encode需要UTF-8编码数据。如果数据不是UTF-8编码,则返回null。确保您的数据是UTF-8编码的。有关您需要的所有信息,请参阅UTF-8 all the way throughHandling Unicode Front To Back In A Web App