Linux上的PHP 5.4:如何与MS SQL Server 2008连接? [英] PHP 5.4 on Linux: How to connect with MS SQL Server 2008?

查看:160
本文介绍了Linux上的PHP 5.4:如何与MS 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和charset 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).

FRIST,我尝试为Linux使用Microsoft SQL Server ODBC驱动程序. 它说仅适用于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.

第二,我尝试对FreeTDS使用PDO驱动程序.这可以正常工作,我可以在我的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,将表更改为使用由VARCHAR插入的NVARCHAR,并可以插入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

它解决了问题! ;)

这篇关于Linux上的PHP 5.4:如何与MS SQL Server 2008连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆