mySQL/PHP JSON 去除特殊字符
我有一个简单的 PHP 脚本来从 mySQL 数据库中提取数据并将其编码为 JSON。问题在于特殊字符(例如德语 ä 或 ß 字符)从 JSON 响应中删除。任何单个字段的第一个特殊字符之后的所有内容都将被删除。
这些字段设置为 utf8_bin,并且在 phpMyAdmin 中字符显示正确。
PHP 脚本如下所示:
<?php
header("Content-type: application/json; charset=utf-8");
$con = mysql_connect('database', 'username', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sql01_5789willgil", $con);
$sql="SELECT * FROM weightevent";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$events = array();
while($row = mysql_fetch_array($result))
{
$eventid = $row['eventid'];
$userid = $row['userid'];
$weight = $row['weight'];
$sins = $row['sins'];
$gooddeeds = $row['gooddeeds'];
$date = $row['date'];
$event = array("eventid"=>$eventid, "userid"=>$userid, "weight"=>$weight, "sins"=>$sins,
"gooddeeds"=>$gooddeeds, "date"=>$date);
array_push($events, $event);
}
$myJSON = json_encode($events);
echo $myJSON;
mysql_close($con);
?>
示例输出:
[{"eventid":"2","userid":"1","weight":"70.1","sins":"Weihnachtspl","gooddeeds":"situps! lots and lots of situps!","date":"2011-01-02"},{"eventid":"3","userid":"2","weight":"69.9","sins":"A second helping of pasta...","gooddeeds":"I ate lots of salad","date":"2011-01-01"}]
-->在第一个记录中,字段“sins”的值应为“Weihnachtsplätzchen”。
非常感谢!
I have a simple PHP script to extract data from a mySQL database and encode it as JSON. The problem is that special characters (for example German ä or ß characters) are stripped from the JSON response. Everything after the first special character for any single field is just stripped.
The fields are set to utf8_bin, and in phpMyAdmin the characters display correctly.
The PHP script looks like this:
<?php
header("Content-type: application/json; charset=utf-8");
$con = mysql_connect('database', 'username', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sql01_5789willgil", $con);
$sql="SELECT * FROM weightevent";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$events = array();
while($row = mysql_fetch_array($result))
{
$eventid = $row['eventid'];
$userid = $row['userid'];
$weight = $row['weight'];
$sins = $row['sins'];
$gooddeeds = $row['gooddeeds'];
$date = $row['date'];
$event = array("eventid"=>$eventid, "userid"=>$userid, "weight"=>$weight, "sins"=>$sins,
"gooddeeds"=>$gooddeeds, "date"=>$date);
array_push($events, $event);
}
$myJSON = json_encode($events);
echo $myJSON;
mysql_close($con);
?>
Sample output:
[{"eventid":"2","userid":"1","weight":"70.1","sins":"Weihnachtspl","gooddeeds":"situps! lots and lots of situps!","date":"2011-01-02"},{"eventid":"3","userid":"2","weight":"69.9","sins":"A second helping of pasta...","gooddeeds":"I ate lots of salad","date":"2011-01-01"}]
--> in the first record the value for field 'sins' should be "Weihnachtsplätzchen".
thanks very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mysql_set_charset("utf8");
就在 mysql_connect 之后mysql_set_charset("utf8");
right after mysql_connect