Alexander Jovric
Alexander Jovric

Reputation: 57

PHP not outputting special characters properly

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

Upvotes: 0

Views: 43

Answers (1)

Johannes
Johannes

Reputation: 67799

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)

Upvotes: 1

Related Questions