Linux 上的 PHP 5.4:如何连接 MS SQL Server 2008?

2021-12-29 00:00:00 utf-8 php zend-framework sql-server-2008

我有一台 Linux 服务器 Debian 6,安装了 Apache 2.2 和 PHP 5.4.我需要将我的应用程序与 MS SQL Server 2008 连接.

I have a Linux server Debian 6, with Apache 2.2 and PHP 5.4 installed. I need to connect my application with a MS SQL Server 2008.

我的应用程序使用 Zend Framework 1.11 和字符集 UTF-8(我将拥有来自世界各地的用户,他们将使用自己的语言放置数据).

My application is using Zend Framework 1.11 and charset UTF-8 (I'll have users from all places in the world and they will put data in their own language).

首先,我尝试将 Microsoft SQL Server ODBC 驱动程序用于 Linux.它说仅适用于 Red Hat,但我按照以下说明进行安装:

FRIST, I tried to use Microsoft SQL Server ODBC driver for Linux. It says is only for Red Hat, but I follow these instructions to install:

http://www.codesynthesis.com/~boris/blog/2011/12/02/microsoft-sql-server-odbc-driver-linux/

我可以连接并对其进行一些选择,但无法在其上插入数据.我在 pdo 语句上绑定参数时遇到问题.

I could connect and make some selects on it, but I couldn't insert data on it. I got a problem on binding parameters on pdo statements.

插入如下数据无效:

$stmt = $conn->prepare("insert into mar_regions (name) values (:name)");
$resp = $stmt->execute(array(':name' => $param));

但是如果我像这样使用它,它会起作用:

But if I used like the this, it works:

$stmt = $conn->prepare("insert into mar_regions (name) values ('".$param."')");
$resp = $stmt->execute();

所以我放弃了这个驱动程序,因为如果这样的话,我的应用程序没有 ZF 1.11 将无法工作.

So I gave up from this driver, because my application no ZF 1.11 will not work if this.

第二,我尝试将 PDO 驱动程序用于 FreeTDS.这个工作正常,我可以在我的 ZF 1.11 应用程序中使用.

SECOND, I try to use PDO Driver for FreeTDS. This one works ok and I could use on my ZF 1.11 application.

但是,我又遇到了一个问题:字符集.我将我的 freeTDS.conf 配置为使用 UTF-8,将我的表更改为使用 NVARCHAR insted of VARCHAR,并且可以像这样插入 utf-8 数据:

But then, I got one more problem: charsets. I configure my freeTDS.conf to use UTF-8, change my tables to use NVARCHAR insted of VARCHAR and could insert utf-8 data like this:

$stmt = $dbh->prepare("insert into mar_teste (name) values (N'ンから初・配信 € зеленый банан ÀÀÀÀáááááá')");
$resp = $stmt->execute();

但是,在我的 ZF 1.11 上,我无法在查询中传递这个N"属性!所以我的应用程序仍然没有工作.

But, on my ZF 1.11, I can't pass this 'N' attribute on querys! So my application still didn't work.

如你所见,我什么都试过了.

As you can see I tried everything.

所以我的问题是:如何在 MS SQL Server 2008 上使用 ZF 1.11 字符集 UTF-8 从 linux 连接?

So my question is: How to connect from linux, using ZF 1.11 charset UTF-8, on MS SQL Server 2008?

推荐答案

我的问题的答案是:使用 freeTDS!上面有一个字符集参数:

The answer for my question is: Use freeTDS! Theres a parameter for charset on it:

[MyDSN]
    host = <<ip>>
    port = <<port>>
    # use 8.0 for newer versions of SQLSERVER
        tds version = 8.0
        # text size don't need to be such a high value, its just an example
        text size = 4294967295
        client charset = UTF-8

在 Zend Framework 上,像这样配置您的连接:

On Zend Framework, configure your connection like this:

;; BANCO DE DADOS LINUX
database.adapter                = PDO_MSSQL
database.params.pdoType         = dblib

database.params.host            = MyDSN
database.params.dbname          = <<dbname>>
database.params.username        = <<username>>
database.params.password        = <<passwd>>
database.params.driver_options.charset = UTF-8

database.isDefaultTableAdapter  = true

它解决了问题!;)

相关文章