user2520217 user2520217 - 5 months ago 16
PHP Question

How to update the Unicode data into SQL Server by PHP?

I using sqlsrv driver to connect the Microsoft SQL Sever 2008 R2 by PHP.
But, i tried to update the information by update statement. it doesn't work
It seems that i cannot update the information with non-English words. Such as Chinese word.
The database doesn't updated.

It works.


$query="UPDATE [Management].[dbo].[Employee]
SET [Management].[dbo].[Employee].[name] = 'abc'
WHERE [Management].[dbo].[Employee].[EmpID]=1;";
$stmt=sqlsrv_query( $conn, $query);


It not work.


$query="UPDATE [Management].[dbo].[Employee]
SET [Management].[dbo].[Employee].[name] = '你好'
WHERE [Management].[dbo].[Employee].[EmpID]=1;";
$stmt=sqlsrv_query( $conn, $query);

Answer

use N flags (nvarchar, ntext...) in table field type for unicode support, and add N flag on query

$query = "UPDATE [Management].[dbo].[Employee]
          SET [Management].[dbo].[Employee].[name] = N'你好'
          WHERE [Management].[dbo].[Employee].[EmpID]=1;";

$stmt = sqlsrv_query($conn, $query);

In html page use charset utf8

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