mySQL/PHP JSON 去除特殊字符

发布于 2024-10-09 23:49:27 字数 1412 浏览 2 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁年轻赶紧闹 2024-10-16 23:49:27

mysql_set_charset("utf8"); 就在 mysql_connect 之后

mysql_set_charset("utf8"); right after mysql_connect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文