Home » Php » php – Storing Chinese, Korean, English, etc in MS SQL through SQL Express

php – Storing Chinese, Korean, English, etc in MS SQL through SQL Express

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am using MS SQL 2008 Express to connect to a shared MS SQL 2008 server where I have a database. The default collation for the DB is currently SQL_Latin1_General_CP1_CI_AS. Ultimately, I would like to store English, Korean, Chinese, and any other language imaginable in the DB. I have started off by using the following SQL code (that I found here: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/5d2ea1a2-32e1-4a82-b6e3-17d2b898babc/) to test things out:

create table zhongwen(mingzi nvarchar(10))
go
insert into zhongwen values (N'有方')
insert into zhongwen values (N'李杰')
insert into zhongwen values (N'空炮鸡蛋')
go
select * from zhongwen
go
create procedure zhongwenfind 
 (@mingzi nvarchar(10))
 AS
 SELECT mingzi FROM zhongwen 
 WHERE mingzi  = @mingzi  
go
exec zhongwenfind N'李杰'
go
drop table zhongwen
go
drop procedure zhongwenfind
go

When I run this code in MS SQL 2008 Express, the results display only several thin boxes. If I copy a set of the thin boxes and paste them in here (the stack overflow ask a question textarea), they show up as proper characters (here I go: 空炮鸡蛋). Is it possible to set MS SQL 2008 Express to display them correctly?

Much more importantly, when I run my PHP site that is ultimately supposed to display the characters correctly to the public, I only get question marks (????). I am using mssql_query() to query the DB. I have the following code at the top of my HTML5 HEAD:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

…and the following code in my PHP:

header('Content-Type:text/html; charset=UTF-8');

…but I only see the question marks. So, to sum it all up, 2 questions:

1) How do I make this display correctly in MS SQL 2008 Express?

2) How do I make this display correctly in PHP / HTML?

Thanks in advance!

How to&Answers:

I had same issue in my application, while using accented characters ( getting ???), and I resolved it by changing encoding of database, tables and text fields to utf8_unicode_ci, along with file encodings. In case you are working on a live server, please take backup of db before converting. Also, store your text fields data to blob before updatinng table and fields collation.

Hope it helps!

Answer:

I had similar a requirement, I was able to solve this and am able to display Korean and Japanese
[SQL Server 2008 R2 Express and HTML + ASP.NET]

  1. For Database Collation in SQL Server use SQL_Latin1_General_CP1_CI_AS

  2. While deploying your code on the server/machine – make sure the HTML files are saved as UTF-8 Encoding
    (To check this – open the file > Save As > check the encoding)