1

I'm outputting some multi-lingual data from my database into a JSON object, but the output isn't showing foreign characters, it just shows question marks even though I have the header charset set to utf8 as such:

header('Content-Type: application/json; charset=utf-8');

When I look at the data in phpMyAdmin, it shows the characters correctly. Is there something I'm doing wrong?

Here is the PHP code that is formatting the JSON output:

$numRows = new stdClass();

$mysqli = dbiConnect();
$query = "SELECT * FROM country_codes";
if ($stmt = $mysqli->prepare($query)) {
    /* execute query */
    $stmt->execute();

    /* store result */
    $stmt->store_result();
    $numRows->cc = $stmt->num_rows;

    /* close statement */
    $stmt->close();
}
$mysqli->close();

$count = 0;
$dataCountryCodes = '{';
    $mysqli = dbiConnect();
    $query = "SELECT * FROM country_codes";
    if ($result = $mysqli->query($query)) {
        while($row = $result->fetch_assoc()){
            $count++;
            $rowData = new stdClass();
            $rowData->code = $row['code'];
            $rowData->name = $row['name'];
            $dataCountryCodes = $dataCountryCodes.'"'.$rowData->code.'": {"Code":"'.$rowData->code.'","Country":"'.$rowData->name.'"}';

            if ($count != $numRows->cc) {
                $dataCountryCodes = $dataCountryCodes.',';
            }
        }
    }
    $mysqli->close();
$dataCountryCodes = $dataCountryCodes.'}';
if ($returnCountryCodes == 1) {
    return $dataCountryCodes;
} else {

    header('Content-Type: application/json; charset=utf-8');
    echo ($dataCountryCodes);
}

This is what I am getting:

{"AE": {"Code":"AE","Country":"United Arab Emirates (???????? ???????? ????????)"}}

This is what I got when it was hand coded, this would render fine after I translated the JSON to HTML:

{"AE": {"Code":"AE","Country":"United Arab Emirates (الإمارات العربيّة المتّحدة)"}}
3
  • Are you sure the data's coming out of the database correct, or was properly put into the database in the first place? Your DB tables/fields might be set for utf-8, but your ENTIRE rendering pipeline has to be set for utf-8 as well, including the php<->db connection. phpmyadmin is probably showing it right because it has a properly configured connection, whereas your code isn't Commented Dec 30, 2013 at 4:36
  • @MarcB I added more detail, the data in the DB is fine when I view it in the DB admin tool... Commented Dec 30, 2013 at 4:41
  • What's inside dbiConnect()? I'm guessing that your database connection character set is not defined properly. Commented Dec 30, 2013 at 4:44

2 Answers 2

2

You didn't show dbiConnect function. Check it and try using SET NAMES 'UTF8' after connecting to MySQL:

$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
    die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}

/* change character set to utf8 */
if (!$con->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $con->error);
}

As the manual says:

SET NAMES indicates what character set the client will use to send SQL statements to the server... It also specifies the character set that the server should use for sending results back to the client.

Sign up to request clarification or add additional context in comments.

Comments

0

try this:

$mysqli = dbiConnect();
$mysqli->query("set names utf8")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.