0

My query selects a row from my database and outputs in form of JSON to the browser, but I have encountered a problem that I can't twist my head around: whenever the row that's being selected by my query contains a special character such as "ö", "ä" or "å" the response from php becomes completely blank. Here is my query:

"SELECT email, firstname, lastname, phone, role FROM members WHERE id=? LIMIT 1"

I output it like this:

$data = array(
                    "email" => $email,
                    "firstname" => $firstname,
                    "lastname" => $lastname,
                    "phone" => $phone,
                    "role" => $role
                );
                   echo json_encode($data);
                   exit();

If I manually replace a part of the output with a special character like this:

$data = array(
                    "email" => $email,
                    "firstname" => "Jörgen", <-------------
                    "lastname" => $lastname,
                    "phone" => $phone,
                    "role" => $role
                );
                   echo json_encode($data);
                   exit();

Then it outputs it properly. My config.php contains this:

header('Content-type: text/plain; charset=utf-8');

And my database collation is set to utf8_general ci

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • 1
    Possible duplicate of [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Spoody May 01 '18 at 17:13

1 Answers1

1

Try to include $db_connect->set_charset("utf8"); in your PHP code when connecting to the database. (where $db_connect would contain your database connection)

Johannes
  • 64,305
  • 18
  • 73
  • 130