[英]Only one of several database-columns turning out null in json
I am trying to copy a webservice I have been using before. 我正在尝试复制以前一直使用的Web服务。 It gets data from a mySQL-database and then echos it in json. 它从mySQL数据库获取数据,然后在json中回显。 It has worked many times before but now I can't seem to get it to work. 它以前已经工作了很多次,但是现在我似乎无法使它工作。
My database is set up like this (I know it's in Swedish but hopefully you get it): 我的数据库是这样建立的(我知道它是瑞典语的,但希望您能得到它):
# Kolumn Typ Kollationering Attribut Null Förvalt
1 id int(11) Nej Inget AUTO_INCREMENT
2 desk text utf8_swedish_ci Ja NULL
3 info varchar(4) utf8_swedish_ci Ja NULL
Then my php-page is set up like this: 然后,我的php页面设置如下:
$query = "SELECT *
FROM `$table`
LIMIT 100
";
$result = mysql_query($query,$conn) or die('Errant query: '.$query);
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
The result I'm getting is strange: 我得到的结果很奇怪:
{"posts":[{"post":{"id":"1","desk":"blablabla","info":"4353"}},{"post": {"id":"2","desk":**null**,"info":"3413"}}]}
I'm getting a null result where I actually have something in the database and it seems to be related to the "åäö"-chars in swedish, but that has never been an issue before. 我得到的结果是空的,实际上我在数据库中有什么东西,它似乎与瑞典语中的“åäö” -chars有关,但这以前从来都不是问题。 I actually tried exporting a table from another database where I've tried the same thing and it worked until I started inputting new values. 实际上,我尝试从另一个数据库中导出表,在该数据库中尝试相同的操作,直到我开始输入新值之前,该表一直有效。
Any thoughts? 有什么想法吗?
Edit: This is the two values in the database right now. 编辑:这是现在数据库中的两个值。
desk info
fragannanananana 4353
arhåaöafghsgjädsiahäkömgdsjnö 3413
i solved it. 我解决了。 just had to add mysql_query('SET CHARACTER SET utf8'); 只需添加mysql_query('SET CHARACTER SET utf8'); and it worked like a charm ! 它就像一个魅力!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.