dsbifvcxr458755493 2014-09-28 05:29
浏览 32

通过PHP将Unicode插入MSSQL

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);
> 
> ?>
  • 写回答

1条回答 默认 最新

  • douxin8383 2014-09-28 05:36
    关注

    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').)

    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)