0

I did my first question here abour populating an array to json_encode and pass it to my javascript on client side, but what I figured out is that the problem seems to be related to json_encode and the values that come from my table. The table is filled with varchars, so it's a table with common categorys, like restaurant,shopping,etc.

I'm doing this loop to fill the array ( I had the while loop before but this one got me to understand the error)

$id=array();
for($i=0;$i<$nmr;$i++){
  $row = mysql_fetch_assoc($sql);
  $id[$i]=$row['Tipo'];
}
echo json_encode($id);

So, if I do it like this, my PhP page returns nothing. But if a print_r($id) it prints me the array with all the strings. If I echo $id[1]; it outputs one of the categorys, so I guess it's the json_encode no? I already set my DB to utf-8 because I noticed that json works that way, so what would be the prblem now?

Thanks

EDIT:

Forgot to mention that if I do this:

  $id[0]="ola";
  $id[1]="ccc";
  $id[2]="sd";

  json_encode($id); 

 output: ["ola","ccc","sd"]

it gives me the array on the output. So it only happens when I try to encode the array filled with values from my DB.

2nd Edit

I said this above but I guess it's better if I show it. The array is being populated because if I print_r($id) right after the json_encode($id) I get the populated array.

Array ( [0] => Restaurantes [1] => Cafés [2] => Shopping [3] => Eventos [4] => Hoteis [5] => Oficinas [6] => Combustiveis )
Community
  • 1
  • 1
RubenC
  • 496
  • 1
  • 5
  • 15
  • 1
    Please add current and desired output to your question – u_mulder Nov 23 '14 at 17:16
  • It is in the question, I want the array from json_encode() so I can get it on my javascript from client side (JqueryMobile app ) – RubenC Nov 23 '14 at 17:19
  • Is there anything in your error log? Furthermore, if you try and echo something after json_encode($id); does that get outputted? – Philip Adler Nov 23 '14 at 17:32
  • Yes, if I do, for example, echo $id[4]; after the echo json_encode[$id]; it outputs me one of the categorys – RubenC Nov 23 '14 at 17:54
  • please show us what you get with a `var_dump` for `$id` right after your population loop, before you do the json_encode. Don't make us guess as the content of it when that runs successfully please =) (you don't show what `$nmr` is, for instance, so for all we know, that loop never adds data to $id, even if you in text-only say it does. Code/output or we can't believe you ;) – Mike 'Pomax' Kamermans Nov 23 '14 at 18:04
  • " But if a print_r($id) it prints me the array with all the strings. If I echo $id[1]; it outputs one of the categorys" there's the answer for the question. $nmr is the number of rows and it's fine. – RubenC Nov 23 '14 at 18:14
  • `json_encode` requires its input to be UTF-8 encoded. Likely yours isn't. See the duplicate for how to deal with UTF-8. – deceze Nov 23 '14 at 18:38
  • As I said "I already set my DB to utf-8 because I noticed that json works that way, " – RubenC Nov 23 '14 at 18:54
  • The most common thing to overlook is to also set your **database connection encoding** to `utf8`. Again, see the duplicate. – deceze Nov 23 '14 at 18:58
  • deceze finally got it working. Before making this question I "did" the right thing to fix it as I set my DB to utf-8, but the values that were already on the table weren't utf-8. So I did that and now it's finally working ! Thank you :) – RubenC Nov 23 '14 at 23:29

0 Answers0