Jens Hendar
Jens Hendar

Reputation: 174

Only one of several database-columns turning out null in json

I am trying to copy a webservice I have been using before. It gets data from a mySQL-database and then echos it in 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:

$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. 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

Upvotes: 0

Views: 71

Answers (1)

Fo Nko
Fo Nko

Reputation: 634

i solved it. just had to add mysql_query('SET CHARACTER SET utf8'); and it worked like a charm !

Upvotes: 1

Related Questions