AMEX
AMEX

Reputation: 1

Inserting Unicode to MSSQL via PHP

I'm trying to insert Arabic language to mssql database. database column is set to nvarchar Data is posted through PHP to a mssql stored procedure.

the problem is : data got inserted as މުއްދަތު Þ‡Þ¨Þްތިއުނާފް ÞŠÞ¯Þ‰Þ¬

could any one please help me to get the correct data to be inserted without converting.

>  <?php header('Content-Type: text/html; charset=utf-8');  include_once
> 'connect.php';
> 
>   sqlsrv_query ($conn,"set character_set_client='utf8'");  
>   sqlsrv_query ($conn,"set character_set_results='utf8'"); 
>   sqlsrv_query ($conn,"set collation_connection='utf8_general_ci'");
>   
> 
> 
>    $tsql = 'Exec SP @NO=?,@Date=?,@IN=?,@creason=?';            
> $params = array($empno,$dutydate,$inDT,$outDT,$reason);
> 
>    $stmt = sqlsrv_query($conn,$tsql,$params) ;
>   
>        $dataArray = array();
>        if ($stmt === false)       {           echo 'SQL Error';           print_r(sqlsrv_errors());       }       else        {
>           
>           
>           
>               $dataArray['error'] =array();
>               while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
>                   array_push($dataArray['error'],$row);
>               }
>               sqlsrv_free_stmt($stmt);
>               
>               }    echo json_encode($dataArray);
> 
> ?>

Upvotes: 0

Views: 1928

Answers (1)

Techy
Techy

Reputation: 2654

For the field to be able to store unicode characters, you have to use the type nvarchar (or other similar like ntext, nchar).

To insert the unicode characters in the database you have to send the text as unicode by using a parameter type like nvarchar / SqlDbType.NVarChar.

(For completeness: if you are creating SQL dynamically (against common advice), you put an N before a string literal to make it unicode. For example: insert into table (name) values (N'Pavan').)

Upvotes: 1

Related Questions