0

Apologies if this is a silly question but I am learning at the minute and can not find the solution to this anywhere.

I have a name in my database table containing é. I run my sql query, pull down the data, put it into an array and then attempt to echo the json encode bur I get nothing. I can print_r and see the contents of the sql result, so I know the data has been sent from the database.

I've tried various things now including - json_encode($array, JSON_UNESCAPED_UNICODE). $ut= utf8_encode($contents); making sure the header = header('Content-Type: application/json; charset=utf-8', true,200);

When I use print_r where the é should be in the name I get a black diamond with a question mark in.

So basically I think I need to convert the é to unicode in order to echo the json encode?

Anyone any ideas?

Here is an example of the code I am using after running the sql.

$result = $conn->query($sql);

    $data = array();

    while ($row = $result->fetch_assoc()) {

        $data[] = $row;
    }

    echo json_encode($data);

Maybe this will help someone in future.

I solved it by changing $data[] = $row; to $data[] = array_map("utf8_encode", $row);

DanS
  • 19
  • 7
  • Do not use `utf8_encode()`. It doesn't do what you think it does – Dharman Mar 07 '21 at 22:52
  • Can you clarify a bit where your data is being proven to be shown correct and where it does get confused? A wild guess would be that either your browser messes the data up on showing it to you (so you should take care in html -> ``), or your php cannot handle it. Where do you run your PHP on? May it be (by accident ;) ) a windows machine? So what's your setup? Did you try it running on an "neutral" machine, like some docker php image? Best, Bent – Bent Mar 07 '21 at 17:46
  • Hi Bent, I think i've just sorted it for now. I needed to change $data[] = $row; to $data[] = array_map("utf8_encode", $row); I am getting an output at least not. – DanS Mar 07 '21 at 17:52
  • If I may give a hint: Always try to use more debugging info. This problem sounds in general like something which would be a bit clearer with more debugging info. Try using dump() or the php debugger of your choice. – Bent Mar 07 '21 at 17:58

0 Answers0