普通网友 2018-11-02 10:32
浏览 576

PHP pdo_sqlsrv:默认情况下在MS SQL Server上插入unicode字符串

I have a database PHP application that can work with MySQL, PostgreSQL and SQLite. The application uses PDO and assumes the database content is Unicode.

I am adding the support for MS SQL SERVER using the pdo_sqlsrv driver and the only solution I have found is to modify all the queries (it's really a lot of work) adding the N prefix in front of the strings for the INSERT and UPDATE queries.

Is there any smarter solution to insert unicode by default? Like a PDO or SQL SERVER setting?

Since all the queries are executed by a function (it's my own layer over PDO), I have also thought about adding a regular expression that modifies the queries before the execution but I think it's quite tricky.

Thanks.

  • 写回答

1条回答 默认 最新

  • dscrn1974 2018-11-02 11:15
    关注

    After you set up your connection to the DB you need to add the PDO Attributes:

    $mssql_connection->setAttribute(PDO::SQLSRV_ATTR_ENCODING, PDO::SQLSRV_ENCODING_UTF8);
    

    Reference: https://docs.microsoft.com/en-us/sql/connect/php/pdostatement-setattribute?view=sql-server-2017

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器