user101772
user101772

Reputation: 21

Retrieving chinese characters in database

i have a database that contains both chinese characters and english. when i queried the database to display them on my browser using php, all i get for the chinese characters are gibberish, none readable characters.

what i have tried: ini_set('mssql.charset', 'UTF-8') on the client side i made sure i included the meta tag specifying UTF-8.

am using mssql server language is php

any help will be great. thanks

Upvotes: 2

Views: 2059

Answers (2)

mintedsky
mintedsky

Reputation: 1103

Try setting the character set within the PHP file after you've connected. Then run the query.

<?php 
include('db_access.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

mysqli_set_charset($dbc, "utf8");  // Set the character set here!!

$query = "SELECT chinese FROM my_table";
while ($row = mysqli_fetch_array($query)) { 
echo $row['chinese'].'<br />';
}  
?>

PHP Manual: https://www.php.net/manual/zh/mysqli.set-charset.php

Upvotes: 0

Desolate Planet
Desolate Planet

Reputation: 561

I worked on a Chinese migration project last year and can point you at a few things to look at:

  1. Check you are using NVARCHAR and NCHAR as your DB data types. Do not use VARCHAR/CHAR
  2. I've not used PHP much, but in the majority of languages I've worked in, you need to make sure the appropriate locale is set/supported.

So a few things to check, try outputing some Chinese text on a sample .php file to confirm it is displaying as expected. Next, update your schema with the correct data types. Finally, point your test script at the DB to see if the data is displayed as expected.

Upvotes: 1

Related Questions