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 )
10
  • 1
    Please add current and desired output to your question Commented Nov 23, 2014 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 ) Commented Nov 23, 2014 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? Commented Nov 23, 2014 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 Commented Nov 23, 2014 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 ;) Commented Nov 23, 2014 at 18:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.